我为我的单选按钮创建了隐藏功能。每当用户单击单选按钮时,它将显示/隐藏 div。 JavaScript:
$('input[type="radio"]').click(function () {
if ($(this).attr("id") == "CodeYes") {
$("#time").show();
}
if ($(this).attr("id") == "CodeNo") {
$("#time").hide();
}
});
HTML 表单:
<div class="form-group">
<label class="col-sm-5 control-label">Code Initiated </label>
<div class="col-sm-7">
<label class="radio radio-inline">
<input type="radio" name="CodeInitiated" value="true" id="CodeYes"> Yes</label>
<label class="radio radio-inline">
<input type="radio" name="CodeInitiated" value="false" id="CodeNo"> No</label>
</div>
</div>
<div class="form-group" id="time">
<label class="col-sm-5 control-label">Time </label>
<div class="col-sm-3 ">
<div Class="input-group ">
<input Class="form-control input-sm" id="CodeInitiatedTime" name="CodeInitiatedTime" type="datetime-local" />
</div>
</div>
</div>
当用户单击yes
按钮时,时间div将显示。问题是,当用户点击yes
按钮并在里面的输入框中输入内容时,然后用户点击no
按钮,这意味着他想要取消之前的输入。当表单提交时,输入仍然保留该值并将其带到 Controller 。如何确保仅在单击正确按钮的单选按钮时才提交该值?
最佳答案
隐藏 div
时,您还可以禁用
输入
(反之亦然)
$('input[type="radio"]').click(function () {
if ($(this).attr("id") == "CodeYes") {
$("#time").show();
$("#time").find(":input").prop( "disabled", false ); //enable it back
}
if ($(this).attr("id") == "CodeNo") {
$("#time").hide();
$("#time").find(":input").prop( "disabled", true ); //disable on hide
}
});
不提交禁用的输入。
关于javascript - 提交选中单选按钮的输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48058747/