我想限制將多個項目添加到第二個列表框,我想我非常接近它。請幫助Javascript限制將同一項目多次添加到第二個列表框
<script type="text/javascript">
function CopyFile() {
var firstListBox = document.getElementById('<%= lstFirstBox.ClientID %>');
var secondListBox = document.getElementById('<%= lstTarget.ClientID %>');
for (var i = 0; i < firstListBox.options.length; i++) {
if (firstListBox.options[i].selected) {
for (var j = 0; j < secondListBox.options.length; j++) {
if (firstListBox.options[i].selected == secondListBox.options[j]) {
alert("Multiple selection will not allow");
}
else {
var newOption = document.createElement("option");
newOption.text = firstListBox.options[i].text;
newOption.value = firstListBox.options[i].value;
secondListBox.options[secondListBox.options.length] = newOption;
firstListBox.options[i].selected = false;
}
}
}
}
return false;
}
</script>
當你運行代碼時會發生什麼?任何錯誤? – codebox 2012-08-03 12:20:20
代碼doesnot將項目從第一個列表框複製到另一個.. – Naveen 2012-08-03 12:21:54
你有沒有嘗試過在這一行上使用'appendChild'? 'secondListBox.options [secondListBox.options.length] = newOption;' – jbabey 2012-08-03 12:30:18