tags:

views:

289

answers:

1
<input type="radio" value="G;-;P063P081719;-;84.063" name="awardDetail"/>
<input type="radio" value="G;-;P063P091719;-;84.063" name="awardDetail"/>
<input type="radio" value="G;-;P063Q081719;-;84.063" name="awardDetail"/>

I am not selecting a default checket attribute checked="checked". When I am selecting a radio button, I am making an Ajax call based.

$('input[name=awardDetail]:radio').click(function(){
    var awardinfo = $('input[name=awardDetail]:checked').val();

and sending this awardinfo to the Ajax function and the returned data from server to display in the DIV.

However, my radio button is checked to previous radio button rather than the current selected radio button.

How do I set the checked attribute for this radio button passing the selected index dynamically.

The radio button values group is dynamically generated from backend.

+1  A: 

You can reference the clicked radio button inside the handler using keyword this (or using event.target). For example

$('input[name=awardDetail]:radio').click(function(){ 
    var awardinfo = $(this).val();
    // other stuff here for ajax call, etc
}

or

$('input[name=awardDetail]:radio').click(function(event){ 
    var awardinfo = $(event.target).val();
    // other stuff here for ajax call, etc
}
Russ Cam
This is necessary because at the time your click handler is called, the change of selected button hasn't happened yet. (In fact if your event handler returns false/preventsDefault, the change of radio won't happen.)
bobince
My issue is not with getting the value. I want the radiobutton to be selected. When the page is displayed first there is no radio button selected. When I select first time, one radio button is selected, If i select the other radio button, it is being selected for the time of ajax call and after ajax call the first selected radio button is being selected.
prashanth
I tried this code, var awardinfo = $(this).val();I am able to send to the server the value, but the first radiobutton that is selected after page load, that is the one being selected after the ajax call rather than the new one.
prashanth
@prashanth - it sounds like the problem is not in the code you have posted, but in another piece of code that executes after the ajax call has finished. Can you edit your question to include additional code from the page? What framework/language are you using? ASP.NET, PHP, etc?
Russ Cam
I am using Java as the backend. I am calling a servlet and sending the awardinfo to the servlet, and the response is sent through a jsp, which is displayed in the div. The response is coming correctly based on the selected radio button. But the radio button is reverting back to the first radio button that is selected after the page is loaded.
prashanth
@prasanth - I'm not overly familiar with Java Server Pages, but my guess is that there is some mechanism, either server-side (in the markup sent to the client) or client-side (a JavaScript function executing when the DOM has loaded) that is checking the last radio button
Russ Cam
change works with firefox, In IE I have to click again on the page to work that way.
prashanth
change event fires after the radiobutton loses focus in IE but not in Firefox, and that's why you're seeing this behaviour. Have you checked to see of there is server-side or client-side code setting the radiobutton in the markup sent to the client or when an event happens in the page (e.g. onAjaxSuccess, when DOM has loaded to name a couple).
Russ Cam