如何使用JQuery验证单击单选按钮?

时间:2016-07-08 22:00:09

标签: jquery html

这个Jquery语句有2个函数。第二个功能是答案是我得到问题的地方。它以某种方式没有看到单击单选按钮,我不知道为什么(也许是较新版本的Jquery?)?

JQUERY有2个函数但第二个是问题 - 对于show_action总是返回false:

extension String

HTML:

//[F] button clicked
$(".upload_files").click(function() {
  var disabled = $(this).attr("do_disable");
  fileClick(audit_instance_id, this, disabled);
});

function fileClick(audit_instance_id, data, disabled) {
 var questionID = data.id;
 var show_action = isAnswered(questionID);// This always comes back false
 if (show_action) {
  $('#question_id').val(questionID);
  $('#uploaded_files_' + questionID).toggle();
  //problem after this
  var visible = $('#uploaded_files_' + questionID).is(':visible');
  if (visible === true) {
    var params = {
      'action': 'get_files',
      'audit_instance_id': audit_instance_id,
      'question_id': questionID
    };
    $.post('ajax/perform_audit.php', params, function(data) {
      displayFiles(data, questionID, audit_instance_id, disabled);
    });
    show_action = false;
  }
} else {
  alert("You must answer the Audit question before adding a file.");
}
return false;
}  


function isAnswered(comment_id) {
 var show_action = false;
 $("#options_" + comment_id + " input, #options_" + comment_id + " textarea").each(function(){

//This part seems to be teh issue or the .each above

  if($(this).attr("type") == "checkbox" || $(this).attr("type") == "radio"){
      if($(this).is(':checked')){
          show_action = true;
      }
   } else if($(this).attr("type") == "text" || $(this).is("textarea")){
      if($(this).val()){
          show_action = true;
      }
   }
 });
return show_action;
} 

2 个答案:

答案 0 :(得分:0)

检查它的彻底方法是验证是否检查了任何内容,例如

   $('#trigger').click(function() {
    if (!$("input[type="radio"][name='name']:checked").val()) {
       alert('Nothing is checked!');
        return false;
    }
    else {
      alert('A radio button is checked!');
    }
  });

要使用控制台日志,只需使用例如console.log("nothing is checked");,然后当您按f12时,您将看到消息和任何错误。



$('#trigger').click(function() {
    if (!$("input[type='radio'][name='fruit']:checked").val()) {
       alert('Nothing is checked!');
        return false;
    }
    else {
       var selVal = "";
selVal = $("input[type='radio'][name='fruit']:checked").val();
      alert('A radio button ' + selVal + ' is checked');
    }
  });

//if we take out the [name='fruit'] bit the function will work for both input sets

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
     <input name="fruit" value="banana" id="banana" type="radio">
<label for="banana">banana</label>
<input name="fruit" value="apple" id="apple" type="radio">
<label for="apple">apple</label>
<input name="fruit" value="orange" id="orange" type="radio">
<label for="orange">orange</label>
<br><br>
    <input name="boy" value="Mark" id="Mark" type="radio">
<label for="Mark">Mark</label>
<input name="boy" value="Dan" id="Dan" type="radio">
<label for="Dan">Dan</label>
<input name="boy" value="Harry" id="Harry" type="radio">
<label for="Harry">Harry</label>
<br><br>

<input id="trigger" type="submit" value="Submit">
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

尝试:

if ($(this).checked){
}

您还可以尝试添加一些日志记录以进行开发,以确保进入的component_id符合您的预期。