2011-03-08 44 views
0

我想阻止同等價值的多種選擇,但值=「他人」只能多選防止相同值的多重選擇,但允許值可以在多個

我的代碼是這樣的

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


JS是區分大小寫爲u的問題時說,value="others",並在你的代碼中有寫

if(select.selectedIndex !== "Others") 

檢查和審查

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上的文檔。

+0

此代碼無法在IE 7上運行 – wyman 2011-03-08 13:32:46