jQuery - 检查是否选择了任何选择选项

时间:2021-11-01 14:30:04

Basically I have a select option which pulls all the 'Tour Names' from a database as $touroptions. $touroptions can contain anything from 1-20 values (select options).

基本上我有一个选择选项,它将数据库中的所有“游览名称”作为$ touroptions。 $ touroptions可以包含1-20个值(选择选项)。

What I need to do is have a jQuery function to do as follows so:-

我需要做的是有一个jQuery函数,如下所示: -

If (any option) #sel-destination-tour is selected {
//DO SOMETHING
}
ELSE {
//DO SOMETHING ELSE
}

I'm just not sure how I could do this.

我只是不确定我怎么做到这一点。

HTML

HTML

<select name="sel-destination-tour" id="sel-destination-tour" class="input-select-tour">

    <option value="0"></option>

    <?php echo $touroptions ?>

</select>

6 个解决方案

#1


27  

You can check whether a option by testing the value attribute of the select

您可以通过测试select的value属性来检查是否有选项

if($('#sel-destination-tour').val()){
    // do something
} else {
    // do something else
}

#2


4  

track the change event of select and do your stuffs accordingly

跟踪select的更改事件并相应地执行您的操作

$(function(){

 $("#sel-destination-tour").change(function(){

 if($(this).val() !="0")
  {
    // logic goes here
  }
  else
  {
   // no option is selected
  }
});

});

#3


3  

try this

尝试这个

$('#sel-destination-tour').val() == ""){
    //nothing selected;
}else{
    //something selected;
}

#4


1  

Assuming that you have the first item as shown in your example, this will do it...

假设你有第一个项目,如你的例子所示,这将做...

if ($("#sel-destination-tour").val() != "0") {
    // nothing selected
} else {
    // something selected
}

#5


0  

Check the val() (assuming 0 is no value):

检查val()(假设0没有值):

if ($('#sel-destination-tour').val() != 0) {
   // has a value selected
} 
else {
   // does not have a value selected
}

Example - http://jsfiddle.net/infernalbadger/RrtT7/

示例 - http://jsfiddle.net/infernalbadger/RrtT7/

#6


0  

In my situation val() was returning [] and coming up as true. So I changed it to:

在我的情况下,val()返回[]并且变为真。所以我改成了:

if($('#sel-destination-tour').val()[0]){
    // do something
} else {
    // do something else
}

#1


27  

You can check whether a option by testing the value attribute of the select

您可以通过测试select的value属性来检查是否有选项

if($('#sel-destination-tour').val()){
    // do something
} else {
    // do something else
}

#2


4  

track the change event of select and do your stuffs accordingly

跟踪select的更改事件并相应地执行您的操作

$(function(){

 $("#sel-destination-tour").change(function(){

 if($(this).val() !="0")
  {
    // logic goes here
  }
  else
  {
   // no option is selected
  }
});

});

#3


3  

try this

尝试这个

$('#sel-destination-tour').val() == ""){
    //nothing selected;
}else{
    //something selected;
}

#4


1  

Assuming that you have the first item as shown in your example, this will do it...

假设你有第一个项目,如你的例子所示,这将做...

if ($("#sel-destination-tour").val() != "0") {
    // nothing selected
} else {
    // something selected
}

#5


0  

Check the val() (assuming 0 is no value):

检查val()(假设0没有值):

if ($('#sel-destination-tour').val() != 0) {
   // has a value selected
} 
else {
   // does not have a value selected
}

Example - http://jsfiddle.net/infernalbadger/RrtT7/

示例 - http://jsfiddle.net/infernalbadger/RrtT7/

#6


0  

In my situation val() was returning [] and coming up as true. So I changed it to:

在我的情况下,val()返回[]并且变为真。所以我改成了:

if($('#sel-destination-tour').val()[0]){
    // do something
} else {
    // do something else
}