[英]The .change for radio are not working properly
我的頁面中還有第二件事:
@Html.EditorFor(m => m.Book.HasAt)
哪個是廣播(在EditorTemplates中已更改):
@model bool?
Yes @Html.RadioButton("", true, Model.HasValue && Model.Value == true, htmlAttributes: ViewData["htmlAttributes"])
No @Html.RadioButton("", false, Model.HasValue && Model.Value == false, htmlAttributes: ViewData["htmlAttributes"])
這也是它在頁面上的外觀:
Yes <input id="Book_HasAt" name="Book.HasAt" type="radio" value="True" class="jq-radio">
No <input id="Book_HasAt" name="Book.HasAt" type="radio" value="False" class="jq-radio checked">
根據值,我需要顯示或隱藏一些數據的div。
我有一個腳本:
$("#Book_HasAt").change(function () {
var status = $(this);
if (status.is(":checked")) {
$("#SuppSelect").show();
}
else {
$("#SuppSelect").hide()
}
});
腳本正在運行,但是有問題。 我的收音機有兩個值,是否(是和否)。 默認情況下為false。 由於某些原因, 腳本僅在將false更改為true時才起作用 。 當我將其從true還原為false時,腳本完全沒有反應,因此div不再隱藏。 這對我來說是無法接受的。
我該如何解決這個問題?
使用Classess而不是ID。ID是唯一的..jQuery會對具有此ID的第一個元素采取行動
采用
'input[name="Book.HasAt"]'
代替
"#Book_HasAt"
將id更改為class
$(".jq-radio").change(function () {
var status = $(this);
if (status.val()=="True") {
$("#SuppSelect").show();
}
else {
$("#SuppSelect").hide()
}
});
你可以用這個...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.