JQuery: Get radio value and perform different operations
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 <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
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/
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 {
...
}
}
});
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
.
精彩评论