[英]How to get the radio buttons that are NOT checked (Jquery)?
I'm building a dynamic mobile quiz app with php and mysql. 我正在用php和mysql构建一个动态的移动测验应用程序。 The app contains 10 multiple-choice questions, some (but not all) of which may be radio buttons (the others are drop-down select). 该应用程序包含10个多项选择题,其中一些(但不是全部)可能是单选按钮(其他是下拉选择项)。
I want to ensure that all the radio buttons have been selected before the submit button is enabled. 我想确保启用提交按钮之前,已选中所有单选按钮。 Here's the script I found on SO: 这是我在SO上找到的脚本:
<script type="text/javascript">
$(document).ready( function() {
$('#submitform').prop('disabled', true);
inspectAllInputFields();
});
$('input[type=radio]').change(function() {
inspectAllInputFields();
});
function inspectAllInputFields(){
var count = 0;
$('.radio-button').each(function(i){
if( $(this).val() === '') { // this line doesn't work
count++;
}
if(count == 0){
$('#submitform').prop('disabled', false);
}else {
$('#submitform').prop('disabled', true);
}
});
}
</script>
Here is an extract of the form code (i just used the first few questions, 2 of which are radio and 1 is a select): 以下是表单代码的摘录(我只使用了前几个问题,其中两个是radio,一个是select):
<div class="row">
<div class="col-md-8">
<h5>1. Question 1</h5>
</div>
<div class="col-md-4 questionclass">
<div class="radio">
<ul class="radio-button" name="question1_ul">
<li>
<input type="radio" name="question1" value="true" id="question1_true">
<label for="question1_true">True</label>
</li>
<li>
<input type="radio" name="question1" value="false" id="question1_false">
<label for="question1_false">False</label>
</li>
</ul>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<h5>2. Question 2?</h5>
</div>
<div class="col-md-4 questionclass">
<div class="radio">
<ul class="radio-button" name="question2_ul">
<li>
<input type="radio" name="question2" value="real" id="question2_real">
<label for="question2_real">Real</label>
</li>
<li>
<input type="radio" name="question2" value="fake" id="question2_fake">
<label for="question2_fake">Fake</label>
</li>
</ul>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<h5>3. Question 3 </h5>
</div>
<div class="col-md-4 questionclass">
<select class="form-control bg-silver" name="question3">
<option value="0-10%">0-10%</option>
<option value="11-20%">11-20%</option>
<option value="21-30%">21-30%</option>
<option value="31-40%">31-40%</option>
<option value="41-50%">41-50%</option>
<option value="51-60%">51-60%</option>
<option value="61-70%">61-70%</option>
<option value="71-80%">71-80%</option>
<option value="81-90%">81-90%</option>
<option value="91-100%">91-100%</option>
</select>
</div>
</div>
<!-- other questions deleted -->
<button class="btn btn-primary btn-block " id="submitform" disabled>Compute results</button>
How do I fix the jquery code so that the submit button is only enabled if the user has clicked something for all the radio buttons? 如何修复jquery代码,以便仅当用户单击所有单选按钮的内容时才启用提交按钮?
Use the below javascript 使用以下javascript
$(document).ready( function() {
$('#submitform').prop('disabled', true);
inspectAllInputFields();
});
$('input[type=radio]').change(function() {
inspectAllInputFields();
});
function inspectAllInputFields(){
var count = 0;
$('#submitform').prop('disabled', false);
$('.radio-button').each(function(i){
count = $(this).find('input[type=radio]:checked').length;
if(count == 0){
$('#submitform').prop('disabled', true);
}
});
}
Please check the fiddle link as well 请同时检查小提琴链接
您需要使用.prop()
函数
if($(this).prop('checked') !== false);
You can use jquery is(":checked")
. 您可以使用jquery is(":checked")
。 or you try a better approach by making input fields required Disable button until all required fields are completed 或者您尝试通过将输入字段设置为“ 禁用”按钮,直到所有必填字段都填写完毕,尝试一种更好的方法
<script type="text/javascript">
$(document).ready( function() {
$('#submitform').prop('disabled', true);
inspectAllInputFields();
});
$('input[type=radio]').change(function() {
inspectAllInputFields();
});
function inspectAllInputFields(){
//If both checked enable submit button
if($("input[name=question1]").is(":checked") && $("input[name=question2]").is(":checked")) {
$('#submitform').prop('disabled', false);
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.