Cambiar el texto del valor del cuadro de selección

I got one text box and one select box.

<input type="text" />

<option value=1>Test1</option>
<option value=2>Test2</option>

Now i want when user type anything in text box, to change value=2 text.

I tried this, but looks like don't work.

$(document).ready(function () {
$("#textbox").keyup(function () {



preguntado el 10 de marzo de 12 a las 16:03

5 Respuestas

Apart from the fact that you haven't given the elements IDs, the problem is that you need to access the <option> element (that one has a value of 2 - no hay <select value=2>). Also, IDs are unique, so using select#sync is (should be) the same as #sync:

$("#sync option[value='2']")

respondido 10 mar '12, 17:03

<input type="text" id="textbox" />
   <option value="1">Test1</option>
   <option value="2">Test2</option>

$(document).ready(function () {
    $("#textbox").keyup(function() {
       $("select option[value='2']").text($(this).val());

respondido 10 mar '12, 17:03

puedes probar


provided that the user only types the value that is in range


respondido 10 mar '12, 16:03

$("#text_box").keyup(function() {
 $("#sync option[value='2']").text($(this).val());

​ ​

Here is fiddle showing it working:

It changes select option number 2 text. Is that what you wanted?

respondido 10 mar '12, 17:03

I see a few problems here:

  1. It doesn't make sense to set the innerText menos <select> element; you want to set its value instead,
  2. El #textbox selector wouldn't actually select anything.
  3. If your input did not exactly match an <option>'s value attribute, the first <option> sería seleccionado.

This snippet will select an <option> si es value attribute matches the value of the text input.

$(document).ready(function () {
    $("input").keyup(function () {
        var $select = $("select"),
            value = $(this).val();
        if($select.find('option[value="' + value + '"]').length){

respondido 10 mar '12, 17:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.