的onclick功能,通过JavaScript的添加量 [英] onclick function for adding the amounts by javascript

查看:87
本文介绍了的onclick功能,通过JavaScript的添加量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我目前正在与我的项目addcart。我写的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的:

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:

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()">

任何一个可以给我正确的code?如果我点击这些按钮的数量应与addcart添加量。例如:如果我的addcart量27£那么我选择第二个单选按钮finaly应该显示像32£

Can any one give me the correct code? If I click those buttons that amount should be added with addcart amount. For example: if my addcart amount is 27£ then I choose 2nd radio button finaly it should be display like a 32£.

在此先感谢。

推荐答案

您的语法是关闭的,它应该是 document.make_payment_frm ,而不是在有引号(这对支架符号,例如文件[make_payment_frm] )。此外,标识不应该被重复的,你的投入应该仅仅是:

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
}

你可以在这里测试它。

不过既然你标记这个jQuery的,为何不把它充分利用?除去那些的onclick 处理程序,并添加点击变更处理程序,像这样的:

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
  });
});

你可以在这里的测试版本。

这篇关于的onclick功能,通过JavaScript的添加量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆