我有下面的代碼包含一個下拉列表和兩個文本框與ids「OtherName」和「OtherDesc」。我想從下拉列表中選擇除「other」之外的任何值時刪除/刪除兩個文本框。這是我的代碼,但它不工作任何想法爲什麼?刪除/刪除基於Dropbox選擇的文本框
<div class="form-group">
@Html.LabelFor(model => model.CategoryId, "Category", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CategoryId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
</div>
</div>
<div id="OtherName" class="form-group" >
@Html.LabelFor(model => model.tbl_Category.CategoryName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.tbl_Category.CategoryName, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div id="OtherDesc" class="form-group">
@Html.LabelFor(model => model.tbl_Category.CategoryDesc, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.tbl_Category.CategoryDesc, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
JQUERY
$(document).ready(function() {
//this line fires no matter what
$("#OtherName").hide();
$("#OtherDesc").hide();
$("#CategoryId").change(function() {
var value = document.getElementById("CategoryId").value;
if (value == "1011") {
$("#OtherName").show();
$("#OtherDesc").show();
}
else {
$("#OtherName").remove();
$("#OtherDesc").remove();
}
});
})
在你的情況,你錯過了最初的隱藏和有選擇的元素然後兩次,在你的事件處理程序的內部和外部。或者你手動觸發'change'事件一次。 – eisbehr
@eisbehr:我只修改了選擇處理程序。其他代碼沒有問題:) –