gpt4 book ai didi

jquery - 使用 jQuery 2.0 动态选择选项

转载 作者:行者123 更新时间:2023-12-01 02:31:15 25 4
gpt4 key购买 nike

以下 jsfillde 适用于 jQuery 1.8.3,但不适用于任何较新的版本..并且我找不到解决它的方法。

http://jsfiddle.net/hyBXd/

$("[name=ids] option").each(function() {
if( $(this).attr('value') == 2 ) { $(this).attr('selected','selected'); }
});

有人可以帮我解决这个问题吗?谢谢

最佳答案

尝试使用prop()而不是 attr():

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method

$(function() {
$("[name=ids] option").each(function() {
if( $(this).prop('value') == 2 ) { $(this).prop('selected','selected'); }
});
});

关于jquery - 使用 jQuery 2.0 动态选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16316096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com