<select size="2">
<option selected="selected">Input your option</option>
<option>Input your option</option>
</select>
使用jQuery优雅地取消选项的最佳方法是什么?
<select size="2">
<option selected="selected">Input your option</option>
<option>Input your option</option>
</select>
使用jQuery优雅地取消选项的最佳方法是什么?
当前回答
通常当我使用选择菜单时,每个选项都有一个与之相关的值。 例如
<select id="nfl">
<option value="Bears Still...">Chicago Bears</option>
<option selected="selected" value="Go Pack">Green Bay Packers</option>
</select>
console.log($('#nfl').val()) logs "Go Pack" to the console
Set the value to an empty string $('#nfl').val("")
console.log($('#nfl').val()) logs "" to the console
现在,这并没有从选项中删除选中的属性,但我真正想要的是值。
其他回答
$("#select_id option:selected").prop("selected", false);
非常感谢你的解决方案。
单选组合列表的解决方案非常有效。这是我在很多网站上搜索后发现的。
$("#selectID").attr('selectedIndex', '-1').children("option:selected").removeAttr("selected");
这个问题已经问了一段时间了,我还没有在旧的浏览器上测试过,但在我看来,一个更简单的答案是
$("#selectID").val([]);
.val()也适用于select http://api.jquery.com/val/
一个快速谷歌发现这篇文章,描述了如何在IE中为单个和多个选择列表做你想做的事情。解决方案似乎也很优雅:
$('#clickme').click(function() {
$('#selectmenu option').attr('selected', false);
});
$(option).removeAttr('selected') //replace 'option' with selected option's selector