收音机的 .change 无法正常工作
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 更改为 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()
}
});
你可以使用这个...
我的页面中还有第二件事:
@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 更改为 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()
}
});
你可以使用这个...