检查是否选择了jQuery选项,如果没有选择默认值

使用jQuery,你如何检查选择菜单中是否有一个选项,如果没有,分配一个选择的选项。

(选择生成与一个应用程序的PHP函数的迷宫,我刚刚继承,所以这是一个快速修复,而我得到我的脑袋周围的那些:)

520476 次浏览

查看select元素的selectedIndex。顺便说一下,这是一个普通的DOM,不是特定于jquery的。

不需要使用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>

lencioni的回答是我推荐的。你可以使用"#mySelect option[value='yourDefaultValue']"改变选项('#mySelect option:last')的选择器来选择具有特定值的选项。关于选择器的更多信息

如果你在客户端大量使用选择列表,请检查这个插件: http://www.texotela.co.uk/code/jquery/select/。如果你想看到更多使用选择列表的例子,请查看源代码

我已经遇到了前面提到的texotela插件,这让我像这样解决它:

$(document).ready(function(){
if ( $("#context").selectedValues() == false) {
$("#context").selectOptions("71");
}
});

这是我的函数改变所选的选项。它适用于jQuery 1.3.2

function selectOption(select_id, option_val) {
$('#'+select_id+' option:selected').removeAttr('selected');
$('#'+select_id+' option[value='+option_val+']').attr('selected','selected');
}

这是一个快速的脚本,我发现工作… . result被赋值给一个标签

$(".Result").html($("option:selected").text());

你们选的人太多了。只需按值选择:

$("#mySelect").val( 3 );

简单!默认值应该是第一个选项。完成了!这将引导你使用不显眼的JavaScript,因为JavaScript不需要:)

不引人注目的JavaScript .

<script type="text/javascript">
$(document).ready(function() {
if (!$("#mySelect option:selected").length)
$("#mySelect").val( 3 );


});
</script>
$("option[value*='2']").attr('selected', 'selected');
// 2 for example, add * for every option
$("#select_box_id").children()[1].selected

这是在jquery中检查一个选项是否被选中的另一种方式。这将返回布尔值(True或False)。

[1]是选择框选项的索引

if (!$("#select").find("option:selected").length){
//
}

这个问题已经很老了,有很多观点,所以我只是抛出一些东西,我相信会对一些人有所帮助。

检查一个选择元素是否有任何选定项:

if ($('#mySelect option:selected').length > 0) { alert('has a selected item'); }

或者检查一个select对象是否没有被选中:

if ($('#mySelect option:selected').length == 0) { alert('nothing selected'); }

或者如果你在某种循环中,想要检查当前元素是否被选中:

$('#mySelect option').each(function() {
if ($(this).is(':selected')) { .. }
});

检查一个元素在循环中是否未被选中:

$('#mySelect option').each(function() {
if ($(this).not(':selected')) { .. }
});

这是一些方法。jQuery有许多不同的方法来完成相同的事情,因此通常只需选择一种最有效的方法。

更改选择框上的事件以触发,一旦它发生,则只需拉出所选选项的id属性:-

$("#type").change(function(){
var id = $(this).find("option:selected").attr("id");


switch (id){
case "trade_buy_max":
// do something here
break;
}
});

我在找类似的东西,发现了这个:

$('.mySelect:not(:has(option[selected])) option[value="2"]').attr('selected', true);

这将查找类中尚未选定选项的所有选择菜单,并选择默认选项(在本例中为“2”)。

我尝试使用:selected而不是[selected],但这不起作用,因为总是会选择一些东西,即使没有什么属性

我发现了一个很好的方法来检查,如果选项被选中,并选择一个默认时,它不是。

    if(!$('#some_select option[selected="selected"]').val()) {
//here code if it HAS NOT selected value
//for exaple adding the first value as "placeholder"
$('#some_select option:first-child').before('<option disabled selected>Wybierz:</option>');
}

如果# some_select没有默认选择选项,则.val ()未定义的

如果你需要显式检查每个选项,看看是否有“selected”属性,你可以这样做。否则使用option:selected你将得到第一个默认选项的值。

var viewport_selected = false;
$('#viewport option').each(function() {
if ($(this).attr("selected") == "selected") {
viewport_selected = true;
}
});