我正在尝试将下拉列表的索引设置为 0;如果用户取消选中示例中所示的复选框,但它只是忽略了一些...
$ddlHeader.attr('selectedIndex', 0);
http://jsfiddle.net/abuhamzah/9YM4P/
//html:
Header:
<br /><input id="check" type="checkbox" name="approve" />
<select id="myddl" name="myddl">
<option value="0">select me</option>
<option value="1">One</option>
<option value="2">Twooo</option>
<option value="3">Three</option>
</select>
//js
var $ddls = $('select');
var $check = $('#check');
var $ddlHeader = $("#myddl");
$ddls.prop("disabled", true);
$check .click(function() {
$ddls.prop("disabled", true);
$ddlHeader.prop("disabled", !this.checked);
$ddlHeader.attr('selectedIndex', 0);
});
最佳答案
$ddlHeader.find('option:first').prop('selected', 'selected');
更新:
$('select').prop('selectedIndex', 0);
或者使用更多代码但更灵活:
$('select').each(function() {
$(this).find('option:first').prop('selected', 'selected');
});
关于jquery - 使用 jQuery 设置 DropDown 的 selectedIndex?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10508795/