gpt4 book ai didi

javascript - 当单选按钮选择为否时禁用要求消息

转载 作者:行者123 更新时间:2023-11-29 23:20:45 25 4
gpt4 key购买 nike

当我选中单选按钮“否”时,我试图禁用所需的消息然而,当我提交表单时什么也没有发生,它只是重定向到同一页面并显示所需的消息

更新代码

//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()
})

引用:

https://api.jquery.com/removeAttr/

关于javascript - 当单选按钮选择为否时禁用要求消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50794691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com