Jquery select option not working in chrome mode

I am trying to automatically select a parameter in select by jquery after selecting the value of the parameter I want to select. However, it does not work in Chrome. It works in firefox and IE 9. Why so? Function filling is a value that fills the values. (res function, only resets the values ​​filled in by filling in the function, and does not matter for this question)

function fill(thisValue) {
    $('#inputString').val(thisValue);
    $.post("get.php?op=category", {queryString: ""+thisValue+""}, function(data){
            if(data.length >0) {
                $("#mymenu option[value='"+data+"']").attr('selected', 'selected');
                $('#mymenu').attr("disabled","disabled");
            }
        });
    $.post("get.php?op=name", {queryString: ""+thisValue+""}, function(data){
            if(data.length >0) {
                $('#nameString').val(data);
                $('#nameString').attr("disabled","disabled");
            }
        });
    $.post("get.php?op=author", {queryString: ""+thisValue+""}, function(data){
            if(data.length >0) {
                $('#authorString').val(data);
                $('#authorString').attr("disabled","disabled");                 
            }
        });
    $.post("get.php?op=publisher", {queryString: ""+thisValue+""}, function(data){
            if(data.length >0) {
                $('#publisherString').val(data);
                $('#publisherString').attr("disabled","disabled");
            }
        });


    setTimeout("$('#suggestions').hide();", 200);
}

function res(inputString) {
$('#publisherString').attr("disabled", false);
$('#publisherString').val('');
$('#nameString').attr("disabled",false);
$('#nameString').val('');
$('#authorString').attr("disabled",false);
$('#authorString').val('');
$('#mymenu').attr("disabled",false);
$('#mymenu option').attr('selected', false);
}
+5
source share
3 answers

You can use val () to set the value of an element select.

Edit

$("#mymenu option[value='"+data+"']").attr('selected', 'selected');

to

$("#mymenu").val(data);

Demo script

prop(). attr() .

$('#mymenu option[value="5"]').prop('selected', true)

attr vs prop, jQuery 1.6

-

+9

​​ , , val . , .

$("#mymenu").val(data);
0

, option. . , " " ?

<option value="b">Blue</option>
<option value="k">Black</option>

Suppose blue is currently selected. If you press the letter "B" on the keyboard (case sensitivity does not matter), it will not change for anything. This is true even if you have an onkeydown event that should set a value, for example:

$(function () {
    // Set the value on key down
    $("select[id=stoneColor1]").on("keydown", function(e) {

        var key = String.fromCharCode(e.keyCode);
        for (var i=0; i<stoneColorOptions.length; i++) {
            if (stoneColorOptions[i].keyPress == key) {
                $("#stoneColor1").val(key.toLowerCase());
                break;
            }
        }
    });
});

Recall that the above code will NOT work if I have

<option value="b">Blue</option>
<option value="k">Black</option>

However, it works when I have

<option value="b">(B) Blue</option>
<option value="k">(K) Black</option>

So, a moral story: it seems you also need to take into account the first letter in your option name.

0
source

All Articles