我试图用4个文本类型求和4个条目的总和,但一切都很好,除了单击按钮时,它不会设置总和。就像我在每个文本输入中输入1一样,总和的文本输入应显示为4。谢谢!

这是我的Javascript代码:

(function(){
var oForm = document.forms;

oForm[2].querySelector("input[type='button']").
                                        addEventListener("click",
                                            sommeValeur,
                                            false);
}) ()

function sommeValeur() {

  var aTxt = document.forms[0].tEx1;
  var total = document.forms[0].tEx2;
var txt1  = aTxt[0].value;
var txt2  = aTxt[1].value;
var txt3  = aTxt[2].value;
var txt4  = aTxt[3].value;

total = parseInt(txt1) + parseInt(txt2) + parseInt(txt3) + parseInt(txt4) ;

return true;
}


这是我的html代码:

<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/form.css" />
</head>
<body>
<section>
            <form name="frm1">
                <label> Valeur 1:
                    <input type="text" name="tEx1" />
                </label>
                <label> Valeur 2:
                    <input type="text" name="tEx1" />
                </label>
                <label> Valeur 3:
                    <input type="text" name="tEx1" />
                </label>
                <label> Valeur 4:
                    <input type="text" name="tEx1" />
                </label>
            </form>
        </section>

        <section>
                <form name="frm2">
                <label> Somme:
                    <input type="text" name="tEx2" />
                </label>
                </form>
        </section>

        <section>
                <form name="frm3">
                <label>
                    <input type="button"
                    value="bouton"
                    name="btn1" /></br>
                </label>
                </form>
        </section>
</body>
<script src="js/exercise4.js"></script>
</html>

最佳答案

看起来您输入的total是第二种形式,因此您需要form[1],并且还需要使用total.value来设置值:

var total = document.forms[1].tEx2;
...
total.value = parseInt(txt1) + parseInt(txt2) + parseInt(txt3) + parseInt(txt4) ;


现场示例:



(function(){
var oForm = document.forms;

oForm[2].querySelector("input[type='button']").
                                        addEventListener("click",
                                            sommeValeur,
                                            false);
}) ()

function sommeValeur() {

  var aTxt = document.forms[0].tEx1;
  var total = document.forms[1].tEx2;
  var txt1  = aTxt[0].value;
  var txt2  = aTxt[1].value;
  var txt3  = aTxt[2].value;
  var txt4  = aTxt[3].value;

  total.value = parseInt(txt1) + parseInt(txt2) + parseInt(txt3) + parseInt(txt4) ;

  return true;
}

<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/form.css" />
</head>
<body>
<section>
            <form name="frm1">
                <label> Valeur 1:
                    <input type="text" name="tEx1" />
                </label>
                <label> Valeur 2:
                    <input type="text" name="tEx1" />
                </label>
                <label> Valeur 3:
                    <input type="text" name="tEx1" />
                </label>
                <label> Valeur 4:
                    <input type="text" name="tEx1" />
                </label>
            </form>
        </section>

        <section>
                <form name="frm2">
                <label> Somme:
                    <input type="text" name="tEx2" />
                </label>
                </form>
        </section>

        <section>
                <form name="frm3">
                <label>
                    <input type="button"
                    value="bouton"
                    name="btn1" /></br>
                </label>
                </form>
        </section>
</body>
</html>

关于javascript - 用4个值和一个按钮求和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36046717/

10-11 23:57