javascript 在 getElementsByTagName 中只选择 Checkbox 输入,排除 Textbox 输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15191384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select only Checkbox input in getElementsByTagName, exclude the Textbox input
提问by Herland Cid
I have one problem. The systems works fine when I select the checkboxes, the Total value changes, but as soon as I modify the text input, it changes to 0. I need to exclude this textbox input from changing the value.
我有一个问题。当我选择复选框时,系统工作正常,总值会发生变化,但是一旦我修改文本输入,它就会更改为 0。我需要从更改值中排除此文本框输入。
here is the html:
这是html:
<input type="checkbox" id="15000"/>000<br/>
<input type="checkbox" id="9400"/>00<br/>
<label>Quantity</label><input class="text" type="text" maxlength="3" size="2" value="1" name="qty"></input>
<div id="total">00</div>
Here is the javascript:
这是javascript:
var input = document.getElementsByTagName("input");
var total = document.getElementById("total");
var prev;
for(i=0;i<input.length;i++){
input[i].onchange = function(){
if(this.checked){
$("#total").fadeOut(300);
$("#total").fadeIn(300);
prev=parseFloat(total.innerHTML.replace(/[^0-9\,]+/g,"")) + parseFloat(this.id);
total.innerHTML = "$ " + prev.formatMoney(0, ',', '.');
}else{
$("#total").fadeOut(300);
$("#total").fadeIn(300);
prev=parseFloat(total.innerHTML.replace(/[^0-9\,]+/g,"")) - parseFloat(this.id);
total.innerHTML = "$ " + prev.formatMoney(0, ',', '.');
}
}
}
Here is the fiddle: http://jsfiddle.net/M7My9/1/
这是小提琴:http: //jsfiddle.net/M7My9/1/
采纳答案by jonasnas
Add if(this.type=='text')
in the first line of onchange handler to detect the type of the input
if(this.type=='text')
在 onchange 处理程序的第一行添加以检测输入的类型