Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JQuery验证RadioBox的更好方法_Javascript_Jquery_Forms_Validation - Fatal编程技术网

Javascript 使用JQuery验证RadioBox的更好方法

Javascript 使用JQuery验证RadioBox的更好方法,javascript,jquery,forms,validation,Javascript,Jquery,Forms,Validation,我的网页上有几个单选按钮组,这就是我决定验证这些组的原因 function validateAppRXForm() { if($('input[name="creativity"]:checked').length == 0 || $('input[name="intelligence"]:checked').length == 0 || $('input[name="initiative"]:checked')

我的网页上有几个单选按钮组,这就是我决定验证这些组的原因

function validateAppRXForm() {
    if($('input[name="creativity"]:checked').length == 0 ||
    $('input[name="intelligence"]:checked').length == 0 ||
    $('input[name="initiative"]:checked').length == 0 ||
    $('input[name="motivation"]:checked').length == 0 ||
    $('input[name="development"]:checked').length == 0 ||
    $('input[name="vision"]:checked').length == 0 ||
    $('input[name="Integrity"]:checked').length == 0 ||
    $('input[name="Confidentiality"]:checked').length == 0 ||
    $('input[name="Interpersonal"]:checked').length == 0 ||
    $('input[name="speed"]:checked').length == 0 ||
    $('input[name="accuracy"]:checked').length == 0 ||
    $('input[name="ability"]:checked').length == 0 ||
    $('input[name="assessmnt"]:checked').length == 0 ||
    $('input[name="errors"]:checked').length == 0){
       alert('some fields were left blank..');
       // alert('some fields were left blank...');
       return false;
    }
 }
但是我觉得有一种更好的方法可以从数组中传递名称。。所以我决定用单选按钮的名字来做这个

 //create the array by splitting string names.... 
 let arr = String('creativity;intelligence;initiative;motivation;development;vision;Integrity;Confidentiality;Interpersonal;speed;$accuracy;ability;assessmnt;errors').split(';');

//loop through and validate...
function validateAppRXForm() {
   arr.forEach(name => {
      if($('input[name=`${name}`"]:checked').length == 0){
         alert('some fields were left blank..');
         return false;
      }
   });
}

但是它不起作用…

像这样,尽管我建议不要使用未检查的收音机,但如果您想测试无/a/b选择,请改为选择

//通过拆分字符串名创建数组。。。。
让arr=创造性;智力主动权“;分割(“;”);
$(函数(){
$(“表格”)。关于(“提交”,职能部门(e){
if(arr.filter(item=>$(`input[name=${item}]:选中“”).length==0.length>0){
console.log(“未检查某些RAD”);
e、 预防默认值();
}
})
})

创造力:
对
不

智能: 对 不
倡议: 对 不
您可以使用
数组#every()
is(':checked')
作为一种比检查长度更短的写入方法

//通过拆分字符串名创建数组。。。。
让arr=创造性;智力主动权“;分割(“;”);
函数isValid(){
返回arr.every(name=>$(`input[name='${name}']`)。is(':checked'))
}
console.log('On load:',isValid())
//检查每一个
arr.forEach(name=>$(`input[name='${name}']`).first().prop('checked',true))
console.log('选中后:',isValid())

创造力:
对
不

智能: 对 不
倡议: 对 不

@mplungjan它仍然有效,我只是更喜欢使用它…注意
forEach
中的
返回
不会返回到外部函数。使用
some()。。。。。。。您没有在提交时进行验证,但仍然很好