我不知道我的问题是否清楚,但我想添加这3个值(该值由4个不同的单选按钮更改)
<input id="somme1" name ="txtRep1" type="text" value="" />
<input id="somme2" name ="txtRep2" type="text" value="" />
<input id="somme3" name ="txtRep3" type="text" value="" />
当我按下按钮btnSomme
时,我想把这三个的总和扔进sommeTotal <fieldset id="section3">
<input type="button" name="btnSomme" id="btn1" value="Somme" onclick="sommeTotal.value = nb4.value" />
<input id="sommeTtl" name ="sommeTotal" type="text" value="" />
</fieldset>
这是javascript函数
function fctSomme();
{
var nb1 = document.getElementById("somme1").value;
var nb2 = document.getElementById("somme2").value;
var nb3 = document.getElementById("somme3").value;
var nb4 = document.getElementById("sommeTtl").value;
nb4 = nb1 + nb2 + nb3
document.getElementById("sommeTtl").value = nb4;
}
这是感兴趣的完整代码
<section>
<h2>5.3</h2>
<form name="frm3">
<fieldset id="section1">
<input name ="btnrad1" type ="radio" value="10" onclick="txtRep1.value = this.value" />
<input name ="btnrad1" type ="radio" value="15" onclick="txtRep1.value = this.value" />
<input name ="btnrad1" type ="radio" value="20" onclick="txtRep1.value = this.value" />
<input name ="btnrad1" type ="radio" value="25" onclick="txtRep1.value = this.value" />
<input name ="btnrad1" type ="radio" value="30" onclick="txtRep1.value = this.value" />
<input id="somme1" name ="txtRep1" type="text" value="" />
</fieldset>
<fieldset id="section2">
<input name ="btnrad2" type ="radio" value="10" onclick="txtRep2.value = this.value" />
<input name ="btnrad2" type ="radio" value="15" onclick="txtRep2.value = this.value" />
<input name ="btnrad2" type ="radio" value="20" onclick="txtRep2.value = this.value" />
<input name ="btnrad2" type ="radio" value="25" onclick="txtRep2.value = this.value" />
<input name ="btnrad2" type ="radio" value="30" onclick="txtRep2.value = this.value" />
<input id="somme2" name ="txtRep2" type="text" value="" />
</fieldset>
<fieldset id="section3">
<input name ="btnrad3" type ="radio" value="10" onclick="txtRep3.value = this.value" />
<input name ="btnrad3" type ="radio" value="15" onclick="txtRep3.value = this.value" />
<input name ="btnrad3" type ="radio" value="20" onclick="txtRep3.value = this.value" />
<input name ="btnrad3" type ="radio" value="25" onclick="txtRep3.value = this.value" />
<input name ="btnrad3" type ="radio" value="30" onclick="txtRep3.value = this.value" />
<input id="somme3" name ="txtRep3" type="text" value="" />
</fieldset>
<fieldset id="section3">
<input type="button" name="btnSomme" id="btn1" value="Somme" onclick="sommeTotal.value = nb4.value" />
<input id="sommeTtl" name ="sommeTotal" type="text" value="" />
</fieldset>
</form>
</section>
答案 0 :(得分:1)
输入更改时是否要添加?点击一个按钮?在简单的检查中,您的nb1+nb2+nb3
将返回类似“101520”的内容,因为它们被视为字符串而不是整数。尝试使用parseInt
将它们转换为整数,如下所示:
function fctSomme(){
var nb1 = parseInt(document.getElementById("somme1").value, 10);
var nb2 = parseInt(document.getElementById("somme2").value, 10);
var nb3 = parseInt(document.getElementById("somme3").value, 10);
var nb4 = document.getElementById("sommeTtl").value;
nb4.value = nb1 + nb2 + nb3
}
按下按钮:<button type="button" onclick="fctSome">Show total</button>
此处的完整代码:http://jsfiddle.net/sbp9E/3/