English 中文(简体)
Validate radio input form with unknown set of radios
原标题:

HI, I am trying to validate a radio button form that has an unknown amount of radio groups.

So far I have this:

var object = $(".radio:checked");
var length = $( #numq ).val();
if(object.length==length) {
    return true;
} else {    
    var unchecked = $(".radio:not(:checked)").empty();
    var q = "";
    unchecked.each(function(){
        q = q+$(this).parent("div").parent("li").children("div.question").children("b").text()+(",");
    });
    alert("Please answer question "+q);
    return false;
}

Which will check if all groups are checked fine... but when it comes to finding out the actual box that has been missed out, I m having issues.

The markup for my radio buttons (php) is below:

<div class="question"><b><?=$qnum?></b><span><?=$row[ question ]?></span></div>

<div class="a1 answer">
    <input type="radio" class="radio" name="<?=$row[ id ]?>" value="1">
    <label><?=$row[ answer1 ]?></label>
</div>

<div class="a2 answer">
    <input type="radio" class="radio" name="<?=$row[ id ]?>" value="2">
    <label><?=$row[ answer2 ]?></label>
</div>

<div class="a3 answer">
    <input type="radio" class="radio" name="<?=$row[ id ]?>" value="3">
    <label><?=$row[ answer3 ]?></label>
</div>

<div class="a4 answer">
    <input type="radio" class="radio" name="<?=$row[ id ]?>" value="4">
    <label><?=$row[ answer4 ]?></label>
</div>

Any ideas? Thanks!

最佳回答

In your loop where radio are generated, add a variable to count them, put total count in a hidden field, then from script read the value of this hidden field using getelementbyid and run the loop for as much time as this total and validate accordingly.

This way, you know total number of them and validation process becomes easier then.

问题回答

暂无回答




相关问题
Bind Button.IsEnabled to custom validation with XAML?

I am sorry I didn t know how to title my question any better, you name it if you got a good 1. I have an entity Contact. this person has navigation properties: Address, Phones (A collection of Phone)....

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

Rails 101 | validates_currency?

I ve searched high and low, but I could not find a solution, to what I think seems like a very common task. In a form I want to have a text input that accepts currency strings (i.e. $1,000,000 or ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签