使用jQuery,你如何检查选择菜单中是否有一个选项,如果没有,分配一个选择的选项。
(选择生成与一个应用程序的PHP函数的迷宫,我刚刚继承,所以这是一个快速修复,而我得到我的脑袋周围的那些:)
使用jQuery,你如何检查选择菜单中是否有一个选项,如果没有,分配一个选择的选项。
(选择生成与一个应用程序的PHP函数的迷宫,我刚刚继承,所以这是一个快速修复,而我得到我的脑袋周围的那些:)
当前回答
$("#select_box_id").children()[1].selected
这是在jquery中检查一个选项是否被选中的另一种方式。这将返回布尔值(True或False)。
[1]是选择框选项的索引
其他回答
查看select元素的selectedIndex。顺便说一下,这是一个普通的DOM,不是特定于jquery的。
<script type="text/javascript">
$(document).ready(function() {
if (!$("#mySelect option:selected").length)
$("#mySelect").val( 3 );
});
</script>
这是一个快速脚本,我发现工作… . result被分配给一个标签。
$(".Result").html($("option:selected").text());
不需要使用jQuery:
var foo = document.getElementById('yourSelect');
if (foo)
{
if (foo.selectedIndex != null)
{
foo.selectedIndex = 0;
}
}
虽然我不确定您到底想要完成什么,但这段代码对我来说是有用的。
<select id="mySelect" multiple="multiple">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
<option value="4">Fourth</option>
</select>
<script type="text/javascript">
$(document).ready(function() {
if (!$("#mySelect option:selected").length) {
$("#mySelect option[value='3']").attr('selected', 'selected');
}
});
</script>