【问题标题】:Radio Button Javascript单选按钮 Javascript
【发布时间】:2012-09-17 05:35:32
【问题描述】:

我有radiobuttons,如下所示:

Apple
<input type="radio" id="one" name="apple" data-price="10" value="light"/> Light
<input type="radio" id="two" name="apple" data-price="20" value="dark" /> Dark
<input type="text" id="appleqty" name="appleqty" value="" />

Mango
<input type="radio" id="three" name="Mango" data-price="30" value="light"/> Light
<input type="radio" id="one" name="Mango" data-price="40" value="dark" /> Dark
<input type="text" id="Mangoqty" name="Mangoqty" value="" />

Pine Apple
<input type="radio" id="four" name="Pineapple" data-price="50" value="light"/> Light
<input type="radio" id="five" name="Pineapple" data-price="60" value="dark" /> Dark
<input type="text" id="Pineappleqty" name="Pineappleqty" value="" />

Grape
<input type="radio" id="six" name="Grape" data-price="70" value="light"/> Light
<input type="radio" id="seven" name="Grape" data-price="80" value="dark" /> Dark
<input type="text" id="Pineappleqty" name="Pineappleqty" value="" />
​

radiobuttons 在Group 中分隔为(Apple,Mango,Pineapple,Grape)。 我需要添加Price 和他需要的Quantity

示例:

如果用户在radiobutton1 Qty 中单击Dark Apple,则Price 应该是20,并且如果用户将单击的Radio 更改为Light Apple radiobutton,那么价格应该是10 - 20(Previous Price If Checked) = 10。 这可以使用 JavaScript 实现吗?

我尝试过的代码:

function upprice(ref)
{
    var elname = ref.getAttribute("name");
    var qtyname = elname+"qty";
    var price = ref.getAttribute("proprice");
    var qty = parseInt(document.getElementById(qtyname).value)
    var newprice = parseInt(price*qty);
    var olprice = parseInt(document.getElementById("orderpagepriceinstant").innerHTML);
    var totalprice = parseInt(olprice+newprice);
    document.getElementById("orderpagepriceinstant").innerHTML = parseInt(totalprice)
}

【问题讨论】:

  • 是的,有可能。你有没有尝试过?
  • 不尝试就无法学习。
  • @vaahost 你读过哪些关于 JS 的书籍或文章?
  • 请检查我上面的问题我已经添加了我尝试过的 Javascript 代码
  • 请注意,id 应该是唯一的,但您已经使用了两次id="one"(苹果收音机和芒果收音机之一)。

标签: javascript html add


【解决方案1】:

你的输入应该是这样的:

<input type="radio" name="apple" value="10">Light
<input type="radio" name="apple" value="20">Dark
<input type="text" name="appleqty" value="">

您可以在单选按钮上放置一个点击监听器,在数量上放置一个更改监听器来更新价格:

<input type="radio" onclick="updatePrice(this)" ...>
<input type="text" onclick="updatePrice(this)" ...>

更新函数为:

function updatePrice(el) {
  var priceEach, quantity, itemValue;

  if (el.type == 'radio') {
    priceEach = getRBValue(el);
    quantity = el.form[el.name + 'qty'].value;

  } else if (el.type == 'text') {
    quantity = el.value;
    priceEach = getRBValue(el.form[el.name.replace(/qty$/,'')]);
  }

  /* 
     code here to validate the value of quantity
  */

  itemValue = quantity * priceEach;


  /* 
     do something with itemValue
  */

  alert(itemValue);
}

// Get the value of a radio button set
function getRBValue(el) {
  var buttons;

  // See if have been passed a button or button set (NodeList)
  if (el.type == 'radio') {
    buttons = el.form[el.name];
  } else if (typeof el.length == 'number') {
    buttons = el;
  }

  if (buttons) {
    for (var i=0, iLen=buttons.length; i<iLen; i++) {
      if (buttons[i].checked) {
        return buttons[i].value;
      }
    }
  }
}

</script>

标记,您还可以向表单添加点击监听器来进行更新,而不是在每个单选按钮上。您还应该有一个重置按钮,以便用户可以清除表单。

<form ... >
    <input type="radio" name="apple" value="10" onclick="updatePrice(this)">Light
    <input type="radio" name="apple" value="20" onclick="updatePrice(this)">Dark
    <input type="text" name="appleqty" value="" onchange="updatePrice(this)">
    <br>
    <input type="reset">
</form>

【讨论】:

  • 对不起先生它不适合我
  • 这只是一种方法的示例,它对我“有效”。你能解释一下“不工作”吗?错误消息、不正确的值等。请注意,我没有使用 data-price 属性,因为单选按钮已经具有可用于该目的的 value 属性。所以您可能需要将el.value 更改为el.getAttribute('data-price') 等等。
  • 我得到这个错误 el.form is null priceEach = getRBValue(el.form[el.name.replace(/qty$/,'')]); el.form 为空按钮 = el.form[el.name];
  • 将表单控件放入表单中。
【解决方案2】:

这是一个快速的 jQuery 示例:http://jsfiddle.net/FTscC/

(笔记本电脑快死了,明天我会详细说明!)

【讨论】:

  • 它可以工作,但我可以在纯 javascript 中获得它.. 不是在 jquery 中
  • 您可以使用onclick()事件为单选按钮调用javascript方法。并且,使用getAttribute()setAttribute() 读取或修改元素的属性。
猜你喜欢
  • 1970-01-01
  • 2022-12-09
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 2012-09-09
  • 2013-02-04
  • 2012-01-07
相关资源
最近更新 更多