onclick function for adding the amounts by javascript
As I am currently working with my project for addcart. I have written the javascript query for radio button can any one check this either my query is wrong or right?
JavaScript:
function get_radio_value()
{
for (var i=0; i < document."make_payment_frm".rmr.length; i++)
{
if (document."make_payment_frm".rmr[i].checked)
{
var rad_val = document."make_payment_frm".rmr[i].value;
}
}
}
HTML:
<input name="rmr" id="rmr" type="radio" value="3" onclick="get_radio_value()">
<input name="rmr" id="rmr" type="radio" value="5" onclick="get_radio_value()">
<input name="rmr" id="rmr" type="radio" value="10" onclick="get_radio_value()">
Can any one give me the correct code? If I click those buttons that amount should be added with 开发者_StackOverflowaddcart amount. For example: if my addcart amount is 27£ then I choose 2nd radio button finaly it should be display like a 32£.
Thanks in advance.
Your syntax is off, it should be document.make_payment_frm
, not quotes in there (that's for bracket notation, e.g. document["make_payment_frm"]
). Also, IDs shouldn't be repeated, your inputs should just be:
<input name="rmr" type="radio" value="3" onclick="get_radio_value()" />
<input name="rmr" type="radio" value="5" onclick="get_radio_value()" />
<input name="rmr" type="radio" value="10" onclick="get_radio_value()" />
Then matching script to get the value:
function get_radio_value() {
var rad_val = 0;
for (var i=0; i < document.make_payment_frm.rmr.length; i++) {
if (document.make_payment_frm.rmr[i].checked) {
rad_val = document.make_payment_frm.rmr[i].value;
break;
}
}
//use rad_val here to add to whatever
}
You can test it out here.
But since you tagged this jQuery, why not take full advantage of it? Remove those onclick
handlers and add a click
or change
handler, like this:
$(function() {
$("input[name='rmr']").change(function() {
var val = +$(this).val();
//use val, it's a number ready to add to
});
});
You can test that version here.
精彩评论