2016-08-03 51 views
1

我遇到问题。我无法检查使用Javascript/Jquery进行验证时单选按钮的选择。其实我正在生成一些多textarea,单选按钮和动态下拉列表。我的要求是我会填写所有字段,并检查验证。但它没有正确发生。我在下面解释我的代码。无法使用Javascript/Jquery正确验证单选按钮

function validate(){ 
    var x =document.getElementById('ques').value; 
    console.log('value',x); 
    for(var i=0;i<x;i++){ 
     var tid="questions"+ i; 
     var rid='answer_type'+i; 
     var sid="nscale"+i; 
     var scaleid='#scaleid'+i; 
     if(document.getElementById("questions"+ i)){ 
      if(document.getElementById(tid).value==''){ 
       alert('Please enter the question'); 
       return false; 
      }else if($('input[id=answer_type' + i + ']').is(':checked')==false){ 
       alert('Please check the answer type'); 
       return false; 
      }else if($(scaleid).css('display') == 'block'){ 
        if(document.getElementById(sid).value==''){ 
        alert('Please select the scale'); 
        return false; 

        } 
      }else{ 

      } 
     } 
    } 
} 

我的完整的代码是在这里。这里plunkr当用户选择所有单选按钮,并点击validate按钮仍然是其显示警报消息Please check the answer type我的问题是。在这里,我的要求是我将检查所有字段和下拉列表,这些是空白或不在验证case.Please帮助我。

+0

请注意,您不应该混用不同的方法。如果您使用的是jQuery,请不要执行'document ....'。保持方法一致。 – Rajesh

+0

你能否在同一个plunkr链接中使这个正确。 – satya

+0

问题是在脚本中追加单选按钮时,您提供给它的ID是id =“answer_type0”,但同时验证您是否还循环了单选按钮的ID。 –

回答

1

尝试使用addQuestionField()函数中的单选按钮id作为id =“answer_type'+ i +'”而不是id =“answer_type0”。

+0

是的,它工作,谢谢。 – satya