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

时间:2008-09-29 16:47:32

标签: javascript jquery forms dom html-select

使用jQuery,如何检查在选择菜单中是否选择了一个选项,如果没有,则选择其中一个选项。

(选择是在我刚刚继承的应用程序中使用迷宫般的PHP函数生成的,所以这是一个快速解决方法,而我可以解决这些问题:)

18 个答案:

答案 0 :(得分:264)

虽然我不确定你想要完成什么,但这段代码对我有用。

<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>

答案 1 :(得分:29)

不需要为此使用jQuery:

var foo = document.getElementById('yourSelect');
if (foo)
{
   if (foo.selectedIndex != null)
   {
       foo.selectedIndex = 0;
   } 
}

答案 2 :(得分:12)

这个问题已经很久了,并且有很多观点,所以我会在那里抛出一些东西来帮助一些我确定的人。

检查select元素是否包含任何选定的项目:

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

或检查选择是否未选择:

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有许多不同的方法来完成同样的事情,所以你通常只选择哪一个看起来效率最高。

答案 3 :(得分:11)

lencioni's answer是我推荐的。您可以更改选项('#mySelect option:last')的选择器,使用“#mySelect option[value='yourDefaultValue']”选择具有特定值的选项。 More on selectors

如果您正在广泛使用客户端上的选择列表,请查看此插件: http://www.texotela.co.uk/code/jquery/select/。如果您想查看更多使用选择列表的示例,请查看源代码。

答案 4 :(得分:9)

这是我更改所选选项的功能。它适用于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');   
}

答案 5 :(得分:7)

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

});
</script>

答案 6 :(得分:3)

简单!默认值应该是第一个选项。 完成!这会让您看到不引人注目的JavaScript,因为不需要JavaScript:)

Unobtrusive JavaScript

答案 7 :(得分:3)

我已经看到提到的texotela plugin,这让我解决了这个问题:

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

答案 8 :(得分:2)

这是一个我发现有效的快速脚本...... .Result被分配到一个标签。

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

答案 9 :(得分:2)

查看select元素的selectedIndex。顺便说一句,这是一个简单的'DOM',而不是特定于JQuery的。

答案 10 :(得分:1)

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

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

答案 11 :(得分:1)

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

答案 12 :(得分:1)

如果选择了选项,我找到了一种检查的好方法,如果没有,则选择默认值。

    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() 未定义

答案 13 :(得分:0)

$("option[value*='2']").attr('selected', 'selected');
// 2 for example, add * for every option

答案 14 :(得分:0)

$("#select_box_id").children()[1].selected

这是在jquery中选择或不选择选项的另一种方法。这将返回布尔值(True或False)。

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

答案 15 :(得分:0)

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

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

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

答案 16 :(得分:0)

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

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

这将找到类中尚未选择选项的所有选择菜单,并选择默认选项(在这种情况下为“2”)。

我尝试使用:selected代替[selected],但这不起作用,因为总是选择某些内容,即使没有任何属性

答案 17 :(得分:0)

如果您需要明确检查每个选项,以查看是否有任何&#34;已选择&#34;属性你可以做到这一点。否则,使用option:selected,您将获得第一个默认选项的值。

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