使用JavaScript从值中删除dropdownlist的项目



我有以下dropdownlist。

@Html.DropDownList("AttractionsAvaiableAttractions", Model.AvaiableAttractions)

我使用脚本来访问此下拉列表的ID TD元素。如何通过value 从所选项目中删除该dropdownlist 的项目?

$(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);
            }   
        });

请尝试,

var itemToRemove = 'Example';
$("[name='AttractionsAvaiableAttractions '] option[value='"+itemToRemove+"']").remove();

fastionionsavaiableattractions是您下拉的名称。

请找到样品,

<!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>

最新更新