2013-05-17 61 views
0

我正在使用jQuery验证插件,无法让它验证依赖于另一个字段。下面的相同逻辑将验证正常选择,但不是多选。这是我的jFiddle:herejQuery验证多个选择依赖于另一个字段

在小提琴中,我使用的是正常选择,但是当我将其更改为多选时,它会中断。寻找解决方案。谢谢。

的jQuery:

$("#my_form").validate({ 
    rules: { 
     'my_choice_name': { 
      required: { 
       depends: function (element) { 
        //this logic works (tested on another field name/value), just not on the multiple select field: 
        if ($('#my_alt_select_id').val() !== '3') { 
         return true; 
        } else { 
         return false; 
        } 
       } 
      } 
     } 
    } 
}); 

回答

0

具有多选择列表时,将.val()是一个数组。

要查看是否选中了X val,您需要使用indexOf()

if($('#my_alt_select_id').val().indexOf('3') == -1) 
+0

太棒了。按预期工作。谢谢! – Patrick

相关问题