Reputation: 47
I have two different set of radio buttons. Under this I am triggering the passing of value to another file when the second set of radio button is triggered. But the issue is that while passing the value jquery takes value of first radio button in the second set. No matter whichever button you select.
<script type = "text/javascript">
$(document).ready(function(){
$("#sub_category_form_cat_1").click(function()
{
var main_cat=$("#main_cat:checked").val();
var sub_cat_form1=$("input:radio[name='sub-category_form_cat_1']:checked").val();
$.ajax
({
data: {'cat':main_cat,'sub_cat':sub_cat_form1},
url: "ajax_files/events-select.php",
type: 'GET',
success: function(data)
{
$("#displayedresults").html(data);
$('#loading_spinner').hide();
$('#loading_text').hide();
$('#loading_row').hide();
$('#displayedresults').show();
//$('.button_export').show();
//$("a.button_export").attr("href", string);
}
});
return false;
});
});
<tr><td>Category:</td><td>
<input type="radio" name="category_form_cat_1" value="Adults" id="main_cat" /> Adults
<input type="radio" name="category_form_cat_1" value="Kids" id="main_cat" /> Kids</td></tr>
<tr><td>Sub-Category:</td><td>
<input type="radio" name="sub-category_form_cat_1" value="Dance" id="sub_category_form_cat_1"/> Dance
<input type="radio" name="sub-category_form_cat_1" value="Fitness" id="sub_category_form_cat_1"/> Fitness
<input type="radio" name="sub-category_form_cat_1" value="Workshop" id="sub_category_form_cat_1"/> Workshop
<input type="radio" name="sub-category_form_cat_1" value="Events" id="sub_category_form_cat_1"/> Events</td></tr>
Upvotes: 0
Views: 690
Reputation: 31614
You can't have more than one element with a given id
. This will always return the first element with that id
. You need to reference the element within the form
var main_cat = $("input[name='category_form_cat_1']:checked").val();
Since this isn't working, let's try something different
<tr><td>Category:</td><td id="maincat>
<input type="radio" name="category_form_cat_1" value="Adults" /> Adults
<input type="radio" name="category_form_cat_1" value="Kids" /> Kids</td></tr>
<tr><td>Sub-Category:</td><td id="subcat">
<input type="radio" name="sub-category_form_cat_1" value="Dance" /> Dance
<input type="radio" name="sub-category_form_cat_1" value="Fitness" /> Fitness
<input type="radio" name="sub-category_form_cat_1" value="Workshop" /> Workshop
<input type="radio" name="sub-category_form_cat_1" value="Events" /> Events
</td></tr>
And for your JS
var main_cat = $("#maincat").children("input:checked").val();
Upvotes: 1
Reputation: 403
id's are really for single and unique tags, what you really want to do is to give all of the radios class names of "sub_category_form_cat_1" and then use $(".sub_category_form_cat_1").click(function(){});
Upvotes: 0