jquery+validation multiple dropdowns
please advise me how one can validate multiple dropdowns in a form, validation should trigger if same value is selected in different dropdowns.
here, if i select same option from both the dropdown it should trigger开发者_如何学JAVA validation. (i am using jquery validation engine for form validations)
<select name="user[role_id]">
<option value="">Please select</option>
<option value="1" selected="selected">Administrator</option>
<option value="2">User</option>
<option value="3">Editor</option>
</select>
<select name="user[role_id]">
<option value="">Please select</option>
<option value="1" selected="selected">Administrator</option>
<option value="2">User</option>
<option value="3">Editor</option>
</select>
thanks
Easiest way is to map the selections and look for dupes. There are better algorithms to look for dupes (i.e. exit as soon as you detect a duplicate), but I opted to use shorter code since the cost is very low: http://jsfiddle.net/rkw79/vqENk/
function HasDupe(){
a = $('select').map(function(i,o) {
return $(o).val();
});
return (a.length != $.unique(a).length);
}
<select name="user[role_id]">
<option value="0">Please select</option>
<option value="1" selected="selected">Administrator</option>
<option value="2">User</option>
<option value="3">Editor</option>
</select>
<select name="user[role_id]">
<option value="0">Please select</option>
<option value="1" selected="selected">Administrator</option>
<option value="2">User</option>
<option value="3">Editor</option>
</select>
<script type="text/javascript">
$(document).ready(function(e){
if ($('select[name="user[role_id1]"]').val() == $('select[name="user[role_id2]"]').val()) {
alert('select different value');//validation msg
}
});
</script>
精彩评论