How to determine which Radio Button is selected in Form with multiple button groups?
I have a form with two groups of radio buttons. They all have the same name, but in the case where a certain type of radio button is selected, I want to change the content before the next submission.
<form>
<input type="radio" value="ahBkM" id="ahBkM" name="key" class="spam">
<input type="radio" value="ahBkA" id="ahBkA" name="key" class="eggs">
<input type="radio" value="ahBkB" id="ahBkB" name="key" class="eggs">
<input type="radio" value="ahBkC" id="ahBkC" name="key" class="eggs">
</form>
If the form is sumitted with the first radio button is selected (class spam), I don't need to do anything special. But if any class eggs button is selected when the form is submitted, then I need to update the DOM with data coming back from the AJAX POST.
I'm looking for a t开发者_JS百科ruth conditional on whether eggs was selected for the POST (I can put the conditional in the AJAX success function), and if so then I have some code to run.
if ($('input[name=key].eggs:checked').length > 0){
// an egg group is selected.
}
Something like that?
Search for checked eggs:
$(".eggs:checked").length > 0
Use :checked
selector.
e.g:
var checkedRadio = $("[name='key']:checked");
var checkedVal = checkedRadio.val();
if(checkedVal == "eggs"){
//Do Something
} else{
}
精彩评论