我希望防止多次选择相同的值,但值=“其他”只能进行多次选择
我的代码就像这样
function preventDupes( select, index ) {
var options = select.options,
len = options.length;
while( len-- ) {
//options[ len ].disabled = false;
}
//select.options[ index ].disabled = true;
if( index === select.selectedIndex ) {
if(select.selectedIndex !== "Others"){
if(select.selectedIndex != "") {
alert('You\'ve already selected the item "' + select.options[index].text + '".\n\nPlease choose another.');
}
this.selectedIndex = 0;
}
} else if(index === "other") {
}
}
var f61 = select = document.getElementById( 'f61' );
var f62 = select = document.getElementById( 'f62' );
var f63 = select = document.getElementById( 'f63' );
f61.onchange = function() {
preventDupes.call(this, f62, this.selectedIndex );
preventDupes.call(this, f63, this.selectedIndex );
};
f62.onchange = function() {
preventDupes.call(this, f61, this.selectedIndex );
preventDupes.call(this, f63, this.selectedIndex );
};
f63.onchange = function() {
preventDupes.call(this, f62, this.selectedIndex );
preventDupes.call(this, f61, this.selectedIndex );
};
<select name="indication_subject[]" id="f61">
<option value="" selected="selected">Subject </option>
<option value="Accounting"> Accounting</option>
<option value="Afrikaans"> Afrikaans</option>
<option value="Arabic"> Arabic</option>
<option value="other">Others</option>
</select>
<select name="indication_subject[]" id="f61">
<option value="" selected="selected">Subject </option>
<option value="Accounting"> Accounting</option>
<option value="Afrikaans"> Afrikaans</option>
<option value="Arabic"> Arabic</option>
<option value="other">Others</option>
</select>
<select name="indication_subject[]" id="f63">
<option value="" selected="selected">Subject </option>
<option value="Accounting"> Accounting</option>
<option value="Afrikaans"> Afrikaans</option>
<option value="Arabic"> Arabic</option>
<option value="other">Others</option>
</select>
对此有何看法?
答案 0 :(得分:0)
JS是区分大小写的正如您所说的那样value="others"
并且在您的代码中有书面
if(select.selectedIndex !== "Others")
检查并查看
答案 1 :(得分:0)
在您链接的JavaScript代码中,您可能会更改
return $.inArray($(this).val(),arr)>-1;
为:
var value = $(this).val();
return $.inArray(value,arr)>-1 && value != 'other';
在您的代码中,有一个错误。
if(select.selectedIndex !== "Others"){
.selectedIndex
是所选元素的整数索引,而不是字符串。相反,您可能需要.value
。您应该阅读HTML select
element's DOM上的文档。