javascript - 当单选按钮选择为否时禁用需要消息
问题描述
当我选中单选按钮“否”时,我试图禁用所需的消息但是当我提交表单时没有任何反应,它只是重定向到同一页面并显示所需的消息
更新代码
//hide/show chapela Yes/No
$(document).ready(function () {
$("input[name$='Chapel']").click(function () {
var test = $(this).val();
if (test == 'No') {
$("div#hideChapel").hide();
}
else {
$("div#hideChapel").show();
}
});
});
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});
$(document).ready(function () {
var test = $(this).val();
if(test == 'No'){
$("hideChapel").prop("required", false);
}
else {
$("hideChapel").prop("required", true);
}
});
<div class="form-group">
@Html.LabelFor(model => model.Chapel, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
<label class="radio-inline">
@Html.RadioButtonFor(model => model.Chapel, "Yes", new {id="Yes", @class = "styled", htmlAttributes = new { @checked = "true"} })
Yes
</label>
<label class="radio-inline">
@Html.RadioButtonFor(model => model.Chapel, "No", new {id="No", @class = "styled", htmlAttributes = new { @checked = "true" } })
No
</label>
@Html.ValidationMessageFor(model => model.Chapel, "", new { @class = "text-danger" })
</div>
</div>
渲染的 HTML
<label class="radio-inline">
<div class="choice" id="uniform-No"><span class="checked"><input checked="checked" class="styled" htmlattributes="{ checked = true }" id="No" name="Chapel" type="radio" value="No" autocomplete="off"></span></div>
No
</label>
<input class="styled" htmlattributes="{ checked = true }" id="Yes" name="Chapel" type="radio" value="Yes" autocomplete="off">
解决方案
您需要在表单提交时执行此操作,您当前正在设置文档加载和输入点击所需的属性。像这样实现:
$('form').submit(function(e)
{
e.preventDefault(); //stop form submitting straight away
var radio = $('.my-radio');
if (radio.val() === 'foo') {
//disable require msg e.g. (this is an example - not a copy + paste)
$('form input').each(function()
{
$(this).removeAttr('required')
})
}
$(this).submit()
})
参考:
推荐阅读
- jsf - 如果单击 h:commandButton,rich:modalPanel 将不再显示
- firebase - Firebase 云函数仅从您的 Web 应用调用
- python - 为什么使用 .set_index() 会给我一个带括号和逗号的索引?
- python - 是否可以使用 pytest 测试 while True 循环(我尝试超时)?
- android - 无法滚动文本视图内容
- selenium - 为什么我们不写 RemoteWebdriver driver = new ChromeDriver();
- javascript - 有没有办法让渐进式 Web 应用程序在不耗尽所有内存的情况下保存大量数据?
- java - 尽管使用了请求包装器,但 Spring 表单输入值在过滤器中读取一次后返回 null
- spring-boot - 在 maven 中使用 jgitver 和本地 repo 时 spring-boot-dependency 版本无效
- c# - 无法继承构造函数,需要对象引用