Lee
Lee

Reputation: 1280

Jquery watch certain fields for certain values

I have some test code here

<input type="radio" name="group1">1
<input type="radio" name="group1">2
<input type="radio" name="group1">3
<br>
<input type="text" name="text1">
<br>
<input type="radio" name="group2">1
<input type="radio" name="group2">2
<input type="radio" name="group2">3
<br>
<input disabled type="submit">

Please can you tell me if there is a way to watch multiple fields so that if their values changes i can enable a button..

So in short instead of having 3 .change rules watching each other... can't i do one piece of code that watches all 3 and if the values equals a particular something it enables the submit button ?

Thanks

Lee

Upvotes: 0

Views: 302

Answers (5)

korywka
korywka

Reputation: 7653

if i correct understood ur question, here it is:

set classes (for less JS code):

<input type="radio" class="g1-1" name="group1">1
<input type="radio" class="g1-2" name="group1">2
<input type="radio" class="g1-3" name="group1">3
<br>
<input type="text" class="text" name="text1">
<br>
<input type="radio" class="g2-1" name="group2">1
<input type="radio" class="g2-2" name="group2">2
<input type="radio" class="g2-3" name="group2">3
<br>
<input disabled type="submit">

JS:

$(function(){
    $('input').click( function(){
        if ( ($('.g1-2').is(':checked')) && ($('.g2-1').is(':checked')) && ($('.text').val()=="ok" ))
        {
            // event
        }
    });
});

Upvotes: 1

Alessandro Vendruscolo
Alessandro Vendruscolo

Reputation: 14875

If your jQuery selector matches more than one element, when you bind a callback function to an event, that function will be bound to all the elements the selector matches.

Example:

$('input[type="radio"]').change(function() {
    $('body').append('changed');
});

See a working fiddle here

Upvotes: 0

Sang Suantak
Sang Suantak

Reputation: 5265

You can use the click event hander. For e.g.:

$(":radio[name='group1'],:radio[name='group2'],:radio[name='group3']").live("click",function(){
    //do something
});

Upvotes: 2

bdparrish
bdparrish

Reputation: 2764

$(':radio').change(function() {
    if ($(this).attr('name') == 'group2')
        $(':submit').removeAttr('disabled');
});

Upvotes: 2

Duncan Smart
Duncan Smart

Reputation: 32068

Sounds like a candidate for http://knockoutjs.com/ - You associate DOM elements with a client-side view model. When the data model's state changes, the UI updates automatically.

Upvotes: 0

Related Questions