Reputation: 31
I'm having a simple form with few different input types. Basically i'm checking if text, textarea, datetime-local had some values and all radiobuttons were checked. That's the code i'm using
$("#submit_button").click(function() {
var alarm = 0;
$('input[type="text"]').each(function() {
if ($(this).val() == "") alarm = 1;
})
$('textarea').each(function() {
if ($(this).val() == "") alarm = 1;
})
$('input[type="datetime-local"]').each(function() {
if ($(this).val() == "") alarm = 1;
})
$('input[type="radio"]').each(function() {
if (!$(this).is("checked")) alarm = 1;
})
if (alarm == 1) {
$("#failure").show(200);
$("#failure").fadeOut(1500);
} else {
$("#success").show(200);
$("#success").fadeOut(1500);
}
});
#success,
#failure {
display: none;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="reshenie_radio"><input type="radio" class="reshenie_radio" name="reshenie" value="Пройден">1</label>
<label class="reshenie_radio"><input type="radio" class="reshenie_radio" name="reshenie" value="Не пройден">2</label>
<label class="reshenie_radio"><input type="radio" class="reshenie_radio" name="reshenie" value="Продлен">3</label>
<textarea rows="10" cols="100" name="recommendations" value=""></textarea>
<label style="text-align: center;"><input type="datetime-local" >Date</label>
<label><input type="text" class="avg_score" name="avg_score" width="30" height="50" value="">avg.score</label>
<section id="success">
<p>Thanks! Everything is ok!</p>
</section>
<section id="failure">
<p>Please fill all the fields!</p>
</section>
<section id="submit">
<input type="submit" name="submit" id="submit_button" value="send!">
</section>
This code checks all the fields fine, but radiobuttons check is not working.
Upvotes: 0
Views: 3937
Reputation: 145
Don't add .each()
for radio input in this code
By taking your code,
you can do the input checking simply like this,
https://jsfiddle.net/saifudazzlings/wa3n19hw/3/
Just remove .each()
and add
if(!$('input[type="radio"]').is(":checked"))
{
alarm = 1;
}
Upvotes: 0
Reputation: 171669
Your issue is you aren't using :checked
selector in is('checked')
Can also use :checked
selector and length
if(!$('.reshenie_radio:checked').length){
alarm = 1;
}
You can combine all the others also using filter()
var $emptyInputs = $('input[type="text"], textarea, input[type="datetime-local"]').filter(){
return !this.value;
});
if($emptyInputs.length){
alarm = 1;
}
Upvotes: 0
Reputation: 2565
you can check your radio inputs like this (no need for foreach loop):
if(!$('.reshenie_radio').is(':checked')) alarm = 1;
Upvotes: 1