gpt4 book ai didi

javascript - 如何让复选框删除表单中的 'required' 验证

转载 作者:行者123 更新时间:2023-12-02 23:27:29 24 4
gpt4 key购买 nike

我想知道如何让一个复选框删除表单中特定单选按钮组的“必需”验证。

如果选中该复选框,我不希望强制执行“required”属性。

这是我现有的 HTML 的示例:https://codepen.io/anon/pen/PrbOPK

<label for="Group1_notRequired">Check here to make radio buttons in Gorup 1 NOT required:</label> <input id="Group1_notRequired" name="Group1_notRequired" type="checkbox" value="Group1_notRequired" />
<br><br><br>
<label><input name="Group1" required="" type="radio" value="Option1" />Group 1 Option 1</label>
<label><input name="Group1" required="" type="radio" value="Option2" />Group 1 Option 2</label>
<br><br><br>
<label><input name="Group2" required="" type="radio" value="Option1" />Group 2 Option 1</label>
<label><input name="Group2" required="" type="radio" value="Option2" />Group 2 Option 2</label>

最佳答案

您可以使用.removeAttr().attr()删除/添加属性:

$('#Group1_notRequired').on('change', function(e) {
if (this.checked == true) {
$('[name="Group1"]').removeAttr('required');
} else {
$('[name="Group1"]').attr('required', '');
}
console.log($('[name="Group1"]').parent()[0].outerHTML);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<label for="Group1_notRequired">Check here to make radio buttons in Gorup 1 NOT required:</label> <input id="Group1_notRequired" name="Group1_notRequired" type="checkbox" value="Group1_notRequired" />

<br><br><br>

<label><input name="Group1" required="" type="radio" value="Option1" />Group 1 Option 1</label>
<label><input name="Group1" required="" type="radio" value="Option2" />Group 1 Option 2</label>

<br><br><br>

<label><input name="Group2" required="" type="radio" value="Option1" />Group 2 Option 1</label>
<label><input name="Group2" required="" type="radio" value="Option2" />Group 2 Option 2</label>

关于javascript - 如何让复选框删除表单中的 'required' 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56673694/

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