从选择菜单中检查多个值

时间:2014-03-07 15:37:52

标签: javascript jquery

对于网站上的联系表单,我想添加两个选择菜单。我希望隐藏第二个选择菜单,并显示何时单击第一个选择菜单中的一个值。我已经设法让它适用于一个价值,但除了Rohda Raalte'我想添加另一个值。

我尝试了几个选项并搜索了很多主题,但没有答案帮助我解决了这个具体问题。这可能很容易,但我的jquery knowlegde并不是那么好。

提前致谢!

<script type="text/javascript">
$(document).ready(function(){
  var Privileges = jQuery('#locatie');
  var select = this.value;
  Privileges.change(function () {
    if ($(this).val() == 'Rohda Raalte') {
        $('.voetballer').show();
    }
    else $('.voetballer').hide();
  });
});
</script>

5 个答案:

答案 0 :(得分:0)

这样做:

Privileges.change(function() { 
     switch($(this).val(){ 
         case "Rhonda Taalte": 
          $('.voetballer').show(); 
         case "Other Case": 
         $('.voetballer').show(); 
         default: 
          $('.voetballer').hide(); 
         break; 

    }
}); 

答案 1 :(得分:0)

如果要检查多个值,可以将它们存储在一个数组中,并检查所选值是否在数组中:

<script type="text/javascript">
$(document).ready(function(){
  var Privileges = jQuery('#locatie');
  Privileges.change(function () {
    var array = ['Rohda Raalte', 'Another value', 'Yet another value'];
    if (array.indexOf($(this).val()) > -1) {
        $('.voetballer').show();
    }
    else $('.voetballer').hide();
  });
});
</script>

如果要检查两个以上的值,这是一种比or条件更好的处理方法。

答案 2 :(得分:0)

如果我理解正确,您可以通过执行以下操作来检查是否至少选择了一个选项:

if ($("#locatie option:selected").length == 0){
    $('.voetballer').hide();
}else{
    $('.voetballer').show();
}

答案 3 :(得分:0)

您可以尝试使用Switch,例如:

$(document).ready(function(){
  var Privileges = $('#locatie');
  Privileges.change(function () {
    switch($(this).val())
    {
        case 'Rohda Raalte':
            //do some action
            break;
        case 'Another Value':
            //do another action
            break;
        default:
            //do default action
            break;
    }
  });
});

答案 4 :(得分:0)

解决方案应该是这样的,你需要选择:选择为,

$(document).ready(function(){
   $("#locatie").change(function () {    
      if ($("#locatie :selected").val() == 'Rohda Raalte') {
         $('.voetballer').show();
      }
      else {
        $('.voetballer').hide();
     }
 });
});