jquery 选项选择在 chrome 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15195227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery option select not working in chrome
提问by Nuv
I'm trying to auto select the option in select by jquery after fetching the option value that needs to be selected. However it is not working in Chrome. Its working in firefox and IE 9. Why is it so ? function fill is the one that fills in the values. (function res, only resets the values filled by function fill and is not important for this question)
在获取需要选择的选项值后,我试图在 select by jquery 中自动选择选项。但是它在 Chrome 中不起作用。它在 Firefox 和 IE 9 中工作。为什么会这样?函数 fill 是填充值的函数。(函数 res,只重置函数填充填充的值,对这个问题不重要)
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);
}
回答by Arun P Johny
You can use val()to set the value of a select
element.
您可以使用val()来设置select
元素的值。
Change
改变
$("#mymenu option[value='"+data+"']").attr('selected', 'selected');
to
到
$("#mymenu").val(data);
Another solution is to use prop(). The attr()method is used to set the attribute value of the element not to set the property.
另一种解决方案是使用prop()。的ATTR()方法用来设置为不设置该属性的元素的属性值。
$('#mymenu option[value="5"]').prop('selected', true)
You can read more about attr vs prop, as well as the jQuery 1.6 release note
你可以阅读更多关于attr vs prop以及jQuery 1.6 发行说明
回答by Gertjan
When you use data with a typo in the statement below it looks like val is not working. Took me a while to notice.
当您在下面的语句中使用带有拼写错误的数据时,看起来 val 不起作用。我花了一段时间才注意到。
$("#mymenu").val(data);
回答by Bradley Kovacs
Here's something I just noticed concerning the name you set in the option tag. I'll use my code as an example. You see where it says Blue and Black below?
这是我刚刚注意到的有关您在选项标签中设置的名称的内容。我将使用我的代码作为示例。你看到下面的蓝色和黑色了吗?
<option value="b">Blue</option>
<option value="k">Black</option>
Let's say Blue is currently selected. If you push the letter 'B' on your keyboard (case sensitivity doesn't matter) it will not change no matter what. This is true even if you have an onkeydown event that is supposed to set the value, such as:
假设当前选择了蓝色。如果您在键盘上按下字母“B”(区分大小写无关紧要),无论如何它都不会改变。即使您有一个应该设置该值的 onkeydown 事件也是如此,例如:
$(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 I said the above code will NOT work if I have
回想一下,我说过如果我有的话,上面的代码将不起作用
<option value="b">Blue</option>
<option value="k">Black</option>
However, it DOES work when I have
但是,当我有时它确实有效
<option value="b">(B) Blue</option>
<option value="k">(K) Black</option>
So yea, moral of the story: it seems you also have to take into consideration the first letter in your option name.
所以是的,故事的寓意:似乎您还必须考虑选项名称中的第一个字母。