我正在使用C#和SQL Server 2005开发ASP .Net MVC 3应用程序。
在视图中,我有两个下拉列表,它从基础中的表中加载相同的值。
我想创建一个事件,从DropDownList 2中删除在DropDwonList 1中选择的项目。
这是代码:
<%:Html.Label("Poste :")%><%: Html.DropDownListFor(model => model.SelectedPoste, Model.PostesItems, new { @id = "poste" })%>
<%:Html.Label("Poste Suivant :")%><%: Html.DropDownListFor(model => model.PosteSuivantSelected, Model.NextPS, new { @id = "dd" })%>
基于上面的代码,当在“Poste”中选择项目时,应该从“Poste Suivant”中删除它的类似物。
这是我尝试的但却无效:
function test3() {
var dd = document.getElementById('dd');
var posteElement = document.getElementById('poste');
var currentIndexP = posteElement.selectedIndex;
var currentIndexD = dd.selectedIndex;
if (currentIndexP == currentIndexD) dd.removeChild(dd[currentIndex]);
}
我认为问题是:因为我没有在下拉列表中定义'test3()'。
答案 0 :(得分:1)
您可以像在HTML帮助程序中使用onchange
属性一样添加id
属性:
<%:Html.Label("Poste :")%><%: Html.DropDownListFor(model => model.SelectedPoste, Model.PostesItems, new { @id = "poste", onchange = "test3()" })%>
<%:Html.Label("Poste Suivant :")%><%: Html.DropDownListFor(model => model.PosteSuivantSelected, Model.NextPS, new { @id = "dd" })%>
在body标记中添加onload
属性,以便该函数运行以删除默认的选定项:
<body onload="test3()">
然后,如果我们修改javascript以便它根据值而不是索引从第二个列表中删除项目并添加一些代码来存储丢失的选项以重新添加,那么一切都应该很好地工作:
var removedOption = null;
var removedOptionIndex = -1;
function test3() {
var dd = document.getElementById('dd');
var posteElement = document.getElementById('poste');
var currentValue = posteElement.options[posteElement.selectedIndex].value; // this will be the value of the selected option, '2' in this case: <option value="2">Two</option>
var currentText = posteElement.options[posteElement.selectedIndex].text; // this will be the text of the selected option, 'Two' in this case: <option value="2">Two</option>
// add the previously removed option back into the list in its previous position
if (removedOption != null) {
var newOpt = document.createElement("option");
newOpt.value = removedOption.value;
newOpt.innerHTML = removedOption.text;
if (removedOptionIndex < 0) removedOptionIndex = 0;
if (removedOptionIndex > dd.options.length) removedOptionIndex = dd.options.length;
insertOptionToSelect(dd, removedOptionIndex, newOpt);
removedOption = null;
}
for (var i = 0; i < dd.options.length; i++) // loop through all of dd's options
{
if (dd.options[i].value == currentValue) // use this if you want to compare by value
{
removedOption = dd.options[i];
removedOptionIndex = i;
dd.removeChild(dd.options[i]);
break;
}
if (dd.options[i].text == currentText) // use this if you want to compare by text
{
removedOption = dd.options[i];
removedOptionIndex = i;
dd.removeChild(dd.options[i]);
break;
}
}
}
function insertOptionToSelect(select, idx, option) {
var saved = [];
var i;
for (i = 0; i < select.options.length; i++) {
saved.push(select.options[i]);
}
select.options.length = 0;
for (i = 0; i < idx; i++) {
select.options[select.options.length] = saved[i];
}
select.options[select.options.length] = option;
while (i < saved.length) {
select.options[select.options.length] = saved[i++];
}
}
感谢pizzamonster对这个问题How to insert option at specified index of select(Multiple) in html?及其insertOptionToSelect
功能的回答。
要按值或文本进行比较,只需使用相应的if
语句,并随意删除其他变量以便整理。我给你了两个选项,因为我自己是MVC的新手,而且我对渲染的HTML不是100%肯定的。]