I have the following HTML that creates a radio button group using Bootstrap.
<div class="btn-group" data-toggle="buttons-radio">
<button class="btn" type="button" name="rating" value="1">
<img ...>
</button>
<button class="btn" type="button" name="rating" value="2">
<img ...>
</button>
<button class="btn" type="button" name="rating" value="3">
<img ...>
</button>
<button class="btn" type="button" name="rating" value="4">
<img ...>
</button>
</div>
<button class="btn" type="submit" name="submit">Submit</button>
Based on user selection, I want to send the variable in the form from the radio group named "rating" with the value assigned to whatever the selected button's value is. How would I do this in jQuery?
$('button[name="rating"].active').val();
In plain English that selection reads as:
button
elementsname
attribute that has a value of rating (the group)active
(indicating it was selected)Edit based on OP's new question in comments:
To capture the input from the button you will need to use custom script handlers. If your goal is to actually submit this with the form, I would actually suggest against it. Mostly because this is not what a user is expecting in a form. Just use a regular radio button input.
However if you do want to use this, you can do the following:
$('form').submit(function() {
var rating = $('button[name="rating"].active').val();
// get the rest of the values
// submit the form
});
Here's an example with a regular input
vs. the button
and why you shouldn't use the button in a form: http://jsfiddle.net/TxgVe/1/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With