简体   繁体   English

如何获取上一个选定单选按钮的值

[英]How to get the value of the prev selected radio button

Given this markup http://jsfiddle.net/ARPCw/鉴于此标记http://jsfiddle.net/ARPCw/ how can I update the total on change?如何在更改时更新总数?

The problem is that I need the value (data-price) of the previous selected radio button, so I can subtract it to the total amount before to add the new one.问题是我需要上一个选择的单选按钮的值(数据价格),所以我可以在添加新单选按钮之前将其减去总金额。

example fiddle: http://jsfiddle.net/ARPCw/5/小提琴示例: http : //jsfiddle.net/ARPCw/5/

relevant js相关js

var cb = document.getElementsByTagName('input'),
    total = document.getElementById('tprice');

for (var i = 0; i < cb.length; i++) {
    (function(c) {
       c.onclick = function() {
          var bp = 200;
          for (var i = 0; i < cb.length; i++) {
             if (!!cb[i].checked) {
               bp += +cb[i].getAttribute('data-price');  
             }
          }
          total.innerHTML = bp;
       }
    }(cb[i]))        
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM