【问题标题】:If a checkbox is checked, REQUIRE a text field to be filled in. Text field is FAILING: always required regardless of checkbox status如果选中复选框,则需要填写一个文本字段。文本字段失败:无论复选框状态如何,始终需要
【发布时间】:2020-06-12 18:25:26
【问题描述】:

我有一组 3 个复选框。如果标记为“喉舌”的选项被选中,则标记为 mpTxt 的文本字段不得留空以使表单生效。目前,无论检查什么,表单都要求填写 mpTxt 字段。

HTML:

 <div class="col-6">
    <b><u>Interface </u></b><br>
    <input id="F429_interfaceTrachCkBx" class="F429_backupV" name="F429_interfaceTrachCkBx" 
type="checkbox">
    Trach 
    <input id="F429_interfaceMaskCkBx" class="F429_backupV" name="F429_interfaceMaskCkBx" 
type="checkbox">
    Mask 
    <input id="mouthpiece" class="F429_backupV" name="F429_interfaceMouthpieceCkBx" type="checkbox">
    Mouthpiece <br>
</div>

 Mouthpiece Ventilation (MPV) Settings 
 <input id="mpTxt" class="F429_mpvSetting" name="F429_mpvSetting" type="text">

JavaScript:

//REQUIRE MPV TEXT IF CKBX CHECKED

var checkBox = document.querySelector('input[id="mouthpiece"]');
var textInput = document.querySelector('input[id="mpTxt"]');

function toggleRequired() {

if (textInput.hasAttribute('required') !== true) {
    textInput.setAttribute('required','required');
}

else {
    textInput.removeAttribute('required');  
}
}

checkBox.addEventListener('change',toggleRequired,false);

【问题讨论】:

  • 我尝试将您的代码粘贴到 jsfiddle 中,它按预期工作。当我选中喉舌复选框时,文本字段变为必需:jsfiddle.net/nxua2to0
  • 有趣。我的其他 javascript 中的某些东西一定会干扰。谢谢。当我找到修复程序时会更新。
  • 已解决:这是由于 Internet Explorer 存储了 cookie。所有其他浏览器都可以工作。

标签: javascript html checkbox textfield required


【解决方案1】:

此错误是由于 cookie 存储在 Internet Explorer 浏览器中。

【讨论】:

    猜你喜欢
    • 2021-09-11
    • 2016-11-09
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多