简体   繁体   English

Javascript +单选按钮/复选框验证

[英]Javascript + Radio Button / CheckBox Validation

在Plain Ole的Javascript中,我是否需要使用循环遍历单选按钮/复选框以查看是否已选中它们,或者是否有更简单的方法?

Yes. 是。 It may be most helpful to... 可能对...最有帮助

  • ...have them all contained in one element, so you can traverse through its children ...将它们全部包含在一个元素中,因此您可以遍历其子元素
  • ...apply a class name to all of them, to use document.getElementsByClassName ...将一个类名应用于所有这些类,以使用document.getElementsByClassName
  • ...use document.getElementsByTagName (though, the tag is the input tag, so you may get other elements as well) ...使用document.getElementsByTagName (尽管该标记是input标记,所以您也可能获得其他元素)

However, that is just getting the elements; 但是,这仅仅是获得要素。 but as you've stated, you will still need to iterate through each one to do whatever comparison you like. 但是,正如您已经说过的那样,您仍然需要遍历每个对象来进行自己喜欢的比较。 jQuery will allow you to do this kind of thing more easily, but "Plain Ole' Javascript" cannot. jQuery将使您可以更轻松地执行此类操作,但是“ Plain Ole'Javascript”则不能。

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

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