如何将文本框值与javascript相乘

bro*_*ker 1 javascript c# asp.net

我想将两个文本框中的值相乘(txtBox1应包含一个Integer值,txtBox2应包含一个Float值)并将结果放在第三个文本框中.我的代码如下,但它不起作用.调用javascript函数,否则失败.有人可以帮我正确编码:\?谢谢

       //the javascript function   
       function CalculateTotal(id1, id2) {
            var txt1 = document.getElementById(id1);
            var txt2 = document.getElementById(id2);

            var total = txt1 * txt2;
            document.getElementById("txtTotal").value = parseFloat(total);
        }

        //c# code, programmatically adding attribute
        txtBox1.Attributes.Add("onBlur", "CalculateTotal('txtBox1, txtBox2')");
Run Code Online (Sandbox Code Playgroud)

Mik*_*son 5

你应该改变

var total = txt1 * txt2;
Run Code Online (Sandbox Code Playgroud)

var total = txt1.value * txt2.value;
Run Code Online (Sandbox Code Playgroud)

txt1并且txt2是输入元素本身,而不是它们包含的值.

在你的下行你用.value你自己来设置参数;)

[编辑]

正如@Dan Dumitru所指出的,您可以使用parseFloat/parseInt,但如果您的输入字段包含其他文本,十进制标记前的缺失数字,指数表示法等,则此选项更有用.