我有多个选择选项字段。
HTML
<select class="selectRound" name="criteriaRound[]" >
<option selected="true" disabled="true"> - select -</option>
<option value="16">value1</option>
<option value="17">value2</option>
<option value="18">value3</option>
<option value="19">value4</option>
</select>
<select class=" selectRound" name="criteriaRound[]">
<option selected="true" disabled="true"> - select -</option>
<option value="16">value1</option>
<option value="17">value2</option>
<option value="18">value3</option>
<option value="19">value4</option>
</select>
<select class=" selectRound" name="criteriaRound[]">
<option selected="true" disabled="true"> - select -</option>
<option value="16">value1</option>
<option value="17">value2</option>
<option value="18">value3</option>
<option value="19">value4</option>
</select>
现在我正在尝试验证每个人是否具有相同的价值。但是如何避免在验证中首先选择的索引。因为如果我第一次选择一个选项会发生什么,剩下的其他选择字段仍在验证。
https://jsfiddle.net/uhhk8vd3/2/
JS
$(document).on('change','.selectRound', function(e){
var tralse = true;
var selectRound_arr = []; // for contestant name
$('.selectRound').each(function(k, v){
var getVal = $(v).val();
if($.trim(selectRound_arr.indexOf(getVal)) != -1){ //
tralse = false;
//it should be if value 1 = value 1 then alert
//But -select- = -select- are still keep validating, how to prevent this
alert('Contestant cannot be same name');
return false;
}else{
selectRound_arr.push($(v).val());
}
});
if(!tralse){
return false;
}
});
答案 0 :(得分:0)
更新的小提琴在这里https://jsfiddle.net/uhhk8vd3/3/
$(document).on('change', '.selectRound', function(e) {
var tralse = true;
var selectRound_arr = []; // for contestant name
$('.selectRound').each(function(k, v) {
var getVal = $(v).val();
//alert(getVal);
if (getVal && $.trim(selectRound_arr.indexOf(getVal)) != -1) {
tralse = false;
//it should be if value 1 = value 1 then alert, and not those if -select- = -select-. how to avoid those -select-
alert('Contestant cannot be same name');
$(v).val("");
return false;
} else {
selectRound_arr.push($(v).val());
}
});
if (!tralse) {
return false;
}
});