admin管理员组文章数量:1415484
Can someone explain this behavior:
<select id ="test">
<option value="-1">---All---</option>
<option value="1">first</option>
<option value="2">second</option>
</select>
$('#test').val(200);
Using the jQuery 1.10.1 the value of the select is null, but using jQuery 1.9.1 the value is the first option.
jsFiddle
Can someone explain this behavior:
<select id ="test">
<option value="-1">---All---</option>
<option value="1">first</option>
<option value="2">second</option>
</select>
$('#test').val(200);
Using the jQuery 1.10.1 the value of the select is null, but using jQuery 1.9.1 the value is the first option.
jsFiddle
Share Improve this question edited Apr 10, 2014 at 9:14 joker 9909 silver badges23 bronze badges asked Apr 10, 2014 at 9:07 gyosifovgyosifov 3,2234 gold badges28 silver badges42 bronze badges 1- In general, when something doesn't work correctly in an older version and works correctly in a newer version, "they fixed it" is a reasonable guess. :-) – T.J. Crowder Commented Apr 10, 2014 at 9:20
3 Answers
Reset to default 6Using the jQuery 1.10.1 the value of the select is null, but using jQuery 1.9.1 the value is the first option.
It's the fix to bug #13514, fixed in v1.10. Setting an invalid value should clear the select (which it does in v1.10+), not leave it at the default (first) option (v1.9).
I think you want to try,
$('#test').val(2);
none
is selected
(or default selected) if the drop-down value not exists.
Demo
The issue es in Blackberry 4.7
having attributes.value
The change in 1.9 version
and 1.10 version
are
jQuery 1.9.1 hook
jQuery.extend({
valHooks: {
option: {
get: function( elem ) {
// attributes.value is undefined in Blackberry 4.7 but
// uses .value. See #6932
var val = elem.attributes.value;
return !val || val.specified ? elem.value : elem.text;
}
},
And jQuery 1.10 hook
jQuery.extend({
valHooks: {
option: {
get: function( elem ) {
// Use proper attribute retrieval(#6932, #12072)
var val = jQuery.find.attr( elem, "value" );
return val != null ?
val :
elem.text;
}
}
Read #6932 Blackberry 4.7: .val() on empty option fails
May be in jQuery 1.9.1 first is the default option if you provide any non-existing value while jQuery 1.10.1 don't select anything in such case.
You should pass value of option
which you wan to select to val(). 200 is a not present as value for any option.
本文标签: javascriptjQuery 1101 setting non existing value on selectStack Overflow
版权声明:本文标题:javascript - jQuery 1.10.1 setting non existing value on select - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745234574a2648976.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论