简体   繁体   English

javascript添加两个文本框值并显示在第三个

[英]javascript add two textbox values and display in third

How do I in javascript/asp add two textbox values and display in third? 我如何在javascript / asp中添加两个文本框值并显示在第三个? My code is below 我的代码如下

 function fill() {
        var txt8 = document.getElementById("TextBox8").value;
        var txt9 = document.getElementById("TextBox9").value;
        document.getElementById("TextBox10").value = txt8 + txt9;


    }

I have onchange="fill" for both TextBox8 and TextBox9 我对TextBox8和TextBox9都有onchange =“fill”

你必须调用函数, onchange="fill()"注意()

why don't you use jQUery? 你为什么不用jquery? Here is a sample: 这是一个示例:

function fill(){
var total=Number($('#TextBox9').val()) + Number($('#TextBox9').val());
$('#TextBox10').val(total);
}
    function Sum() {
        var text1 = document.getElementById('<%= TextBox1.ClientID %>');
        var text2 = document.getElementById('<%= TextBox2.ClientID %>');
        if (text1.value.length == 0 || text2.value.length == 0) {
            text1.value = 0;
            text2.value = 0;
        }

        var x = parseFloat(text1.value);
        var y = parseFloat(text2.value);          

        document.getElementById('<%= TextBox3.ClientID %>').value = x + y;
    }




<table>
    <tr>
        <td style="width: 100px">
            TextBox1</td>
        <td>
            <asp:TextBox ID="TextBox1" runat="server" onkeyup="Sum()"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            TextBox2</td>
        <td>
            <asp:TextBox ID="TextBox2" runat="server" onkeyup="Sum()"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            TextBox3</td>
        <td>
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;</td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            &nbsp;</td>
        <td>
            <asp:Button ID="Button1" runat="server" Text="Show" Width="80px" OnClientClick="Sum()" />
        </td>
    </tr>
</table>

You need to convert string to integer. 您需要将字符串转换为整数。 value always return string. 值总是返回字符串。 jsfiddle 的jsfiddle

 <input onchange="fill()"  id=TextBox8 />
<input  onchange="fill()"  id=TextBox9 />
<input  id=TextBox10 /> 
<script>

    function fill() {
        var txt8 = document.getElementById("TextBox8").value-0;
        var txt9 = document.getElementById("TextBox9").value-0;
        document.getElementById("TextBox10").value = txt8 + txt9;


    }
</script>

Do you have a set of parentheses following the function call? 函数调用后是否有一组括号? In javascript if you don't follow a function call with parentheses it assumes you are referencing a variable. 在javascript中,如果您不使用括号跟随函数调用,则假定您正在引用变量。 Also in the future if you are struggling to debug javascript code you should open up your browsers javascript console to see what is going on and what errors (if any) are thrown. 此外,如果你正在努力调试javascript代码,你应该打开你的浏览器javascript控制台,看看发生了什么以及抛出了什么错误(如果有的话)。

<input type="text" id="TextBox8" onchange="fill()" />

<input type="text" id="TextBox9" onchange="fill()" />

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

相关问题 javascript添加两个文本框值并在asp.net中显示第三个 - javascript add two textbox values and display in third in asp.net 添加两个文本框值并自动在第三个文本框中显示总和 - Add two textbox values and display the sum in a third textbox automatically 添加两个数字并使用 Javascript 在文本框中显示结果 - Add two numbers and display result in textbox with Javascript 自动组合Javascript中两个文本框的值以在第三个文本框中显示 - Automatically combine values of two text boxes in Javascript to display in a third Javascript-如何添加两个文本框的结果并在第三个文本框中显示结果? - Javascript - How to add results of two text boxes and display the result in a third? 在循环内将两个文本框相乘,并在第三个文本框上显示结果 - Multiply two textbox inside loop and display result on third textbox 如何使用 Javascript 从两个文本框中获取值并在第三个按钮单击时显示? - How to get values from two textboxes and display on third on button click using Javascript? 如何使用JavaScript在asp:textbox中显示值 - How to display values in asp:textbox using javascript 添加三个数字并使用Javascript在文本框中显示结果 - Add three numbers and display result in textbox with Javascript 使用 JavaScript 从文本框中添加两个数字 - Add Two numbers from textbox using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM