我有以下DropDownList。
@Html.DropDownList("AttractionsAvaiableAttractions", Model.AvaiableAttractions)
我使用脚本来访问位于id td元素的这个下拉列表。如何从此下拉列表中删除项目按值而非选定项目?
$(document).ready(function () {
var table = document.getElementById('TimeTableAttractions');
var rowLength = table.rows[0].length;
var row = table.rows[0];
for (var y = 1; y < row.cells.length; y++) {
var $td = $(row.cells[y]).closest("td");
var itemToRemove = 'Example';
$td.find("[name^='AttractionsAvaiableAttractions']").Remove(itemToRemove);
}
});
答案 0 :(得分:0)
请尝试,
var itemToRemove = 'Example';
$("[name='AttractionsAvaiableAttractions '] option[value='"+itemToRemove+"']").remove();
其中,ViewsAvaiableAttractions是您下拉列表的名称。
请找到样本,
<!DOCTYPE html>
<html>
<head>
<script src="Scripts/jquery-3.1.1.min.js"></script>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
function removeItem() {
$("[name='programminglanguages'] option[value='2']").remove();
}
</script>
</head>
<body>
<select id="ddlList" name="programminglanguages">
<option value="www.codewithvijay.com">Code</option>
<option value="2">C#</option>
<option value="3">Java</option>
<option value="4">Go</option>
<option value="5">C++</option>
</select>
<input type="button" onclick='removeItem()' value="Click" />
</body>
</html>