views:

23

answers:

3

Hi there,

I have 2 radio inputs. How can I process jquery functions on a selected value on a onchange event?

<input value="radio_2way" name="IBE1$IBE_NurFlug1$flightIBE" id="IBE1_IBE_NurFlug1_radio_2way" checked="checked" type="radio">Hin- und Rückflug &nbsp;&nbsp;<input value="radio_1way" name="IBE1$IBE_NurFlug1$flightIBE" id="IBE1_IBE_NurFlug1_radio_1way" type="radio">Einfach

If the checked value is radio_1way do function1 and if it is radio_2way do function2.

thx a lot, greetings

A: 

Hi there.

Try

 $("input").click(function(){
            if($(this).attr('value') == 'radio_2way'){
              alert('2 way');
             }    
    });

Change the above code for your needs. You will want to place the above code in the $(document).ready() section.

EDIT:

As pointed out, you likely will need to use $("input").change rather then click. Cheers. Jas.

Jason Evans
A: 

I would use the change event based on the name, then find the value of the button that is checked and key off it. Note that the button being changed will be the one that is checked so you can get the value of this. I was originally concerned that their might be two change events triggered (one for the button being checked and another for the button being unchecked). The two browsers I tested, though, seemed to handle it in a sane way and only generated the change event on the button being checked. Tested in Firefox and Safari.

$(function() {
    $('[name=IBE1$IBE_NurFlug1$flightIBE]').change( function() {
        if ($(this).val() == 'radio_2way") {
           ...
        }
        else {
           ...
        }
    }
});
tvanfosson
no need to repeat the selector, event.target has the domelement.
Jamiec
@Jamiec - yes, I realized as a I posted that the button causing the event would be the one that is checked for a radio button. I've corrected it now.
tvanfosson
A: 

Just attach a change event to the radio's

$(document).ready(function(){
    $('input[id^="IBE1_IBE_NurFlug1"]').change(function(e){
       var $changed = $(e.target);
       alert($changed.val());
    });
});

check the jsFiddle: http://jsfiddle.net/BUgRV/

Jamiec