gpt4 book ai didi

javascript - 如何在 jQuery 中设置下拉列表的 SELECTED 属性?

转载 作者:搜寻专家 更新时间:2023-11-01 04:09:18 24 4
gpt4 key购买 nike

我正在发送用户从索引中选择的下拉选项的索引号。现在想在另一个 View 中将该特定选项选择为“已选择”。如何使用 jQuery 实现?

$('#book_selection').attr("disabled", "disabled");
$('#book_selection').selectedIndex = 1;

但它不起作用...

最佳答案

使用 prop() 方法,将 selectedIndex 设置为 jQuery 对象实际上什么都不做。对于修改属性的 jQuery 1.6 应该使用 prop 方法而不是 attr 方法:

$('#book_selection').prop("disabled", true)
.prop('selectedIndex', 1);

备选方案:

// Getting the DOM element object using bracket notation and `.get()` method
$('#book_selection')[0].selectedIndex = 1;
$('#book_selection').get(0).selectedIndex = 1;

关于javascript - 如何在 jQuery 中设置下拉列表的 SELECTED 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20867977/

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