简体   繁体   English

JavaScript,如果复选框未选中

[英]JavaScript if check-box is checked not working

I have a website form where I have fields that I want to be required (with JQuery Plugin) only when the check-box is checked. 我有一个网站表单,其中只有选中该复选框时,我才需要我的字段(使用JQuery插件)。

So far I wrote JavaScript that works if the the check-box is not checked, or checked. 到目前为止,我编写了JavaScript,如果未选中该复选框,则该JavaScript可以工作。 But when a user checks the check-box and then unchecks it the code does not work (field is still mandatory when unchecked). 但是,当用户选中复选框然后取消选中该复选框时,代码将不起作用(未选中时该字段仍为必填字段)。

Also one other bug is for some reason this same code only works for updating only one items required boolean. 另外还有一个错误是由于某种原因该相同的代码仅适用于仅更新布尔值所需的一项。 For example if I have the same code but make it so two elements requirements are updated the script does not work at all. 例如,如果我具有相同的代码,但要使它更新为两个元素要求,则该脚本根本不起作用。

Here is the relevant source code: 这是相关的源代码:

<script>
function swap(){
  if(document.getElementById('must').checked){     
    document.getElementById("element1").setAttribute("required", "true");
  }else{      
    document.getElementById("element1").setAttribute("required", "false");
  }
}
</script>


<form>
<input type="checkbox" onclick="swap();" id="must" name="must" value="1" style="width:10;">
<input id="element1" type="text" name="element1" placeholder="Enter Value" >
<input type="submit" id="submit" name="submit" value="Send" >
</form>     

Any assistance would be greatly appreciated. 任何帮助将不胜感激。

Best way I know to check if checkbox is checked , it to use jQuery is() method ( documentation ). 我知道检查checkbox是否被checked ,它使用jQuery is()方法( 文档 )。 It handles x-browser differences for you. 它可以为您处理x浏览器差异。 Also best way to add attribute to HTML element is to use attr() method. 向HTML元素添加属性的最佳方法是使用attr()方法。

if($("#must").is(":checked")) {
    $("#element1").attr('required', 'required');
} else {
    $("#element1").removeAttr();
}

You can check in DOM inspector that attribute is added. 您可以在DOM检查器中签入已添加的属性。

Required attribute documentation is here . 所需的属性文档在此处

Try this, in xhtml we specify required="required" and there no required="false", a better way is to remove attribute required on else 尝试此操作,在xhtml中,我们指定required =“ required”,并且没有required =“ false”,更好的方法是删除else上的required属性

<script type="text/javascript">
   function swap(){
  if(document.getElementById('must').checked){     
    document.getElementById("element1").setAttribute("required", "required");
  }else{      
    document.getElementById("element1").removeAttribute("required");
  }
}
    </script>

The required attribute is a boolean attribute, it presence means that the field is required and its absence means the field isn't required. required属性是布尔属性,它的存在表示该字段是必填字段,而它的不存在表示该字段不是必填字段。 It has only 3 valid forms 1. required alone by itself 2. required="" equal to an empty string 3. required="required" 它只有3有效形式1. required通过本身单独2. required=""等于一个空字符串3. required="required"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM