如何在asp.net MVC中使用Javascript,基于在另一个下拉列表中选择的项,从下拉列表中删除一个项
本文关键字:下拉列表 选择 一个 删除 MVC net asp Javascript 另一个 | 更新日期: 2023-09-27 18:15:38
我正在使用c#和SQL Server 2005开发一个asp.net MVC 3应用程序。
在视图中,我有两个下拉列表,它们从基表中加载相同的值。
我想创建一个事件,从DropDownList 2中删除droppdwonlist 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" })%>
因此,根据上面的代码,当项目在' post '中被选中时,它的相似之处应该从' post '中删除。
这是我所尝试的,但它不工作:
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()'。
您可以像在HTML帮助器中使用id
属性一样添加onchange
属性:
<%: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++];
}
}
Credit to pizzamonster对这个问题的回答如何在html中选择(多个)的指定索引插入选项?和insertOptionToSelect
函数
要按值或文本进行比较,只需使用适当的if
语句,并随意删除其他变量以保持整洁。我给了你两个选项,因为我是新的MVC我自己,我不是100%确定渲染的HTML =]