我试图从两个文本框中获取值,减去值并在键入值时在标签中返回差值。我尝试了以下代码,但没有运气。做这个的最好方式是什么?这在带有母版页的asp.net页上运行。

.ASPX

  <asp:TextBox ID="txtWeek1FridayAM_IN" runat="server" class="numeric" Width="40px" >0.00</asp:TextBox>
  <asp:Label ID="lblWeek1FridayTotalHrs" runat="server" class="numeric" Text="0"></asp:Label>
  <asp:TextBox runat="server" ID="txtWeek1FridayAM_OUT" class="numeric" width="40px"  OnTextChanged="OUTvalidation" >0.00</asp:TextBox>


aspx.cs

     $(document).ready(function () {
        sum();
        $("#txtWeek1FridayAM_IN, #txtWeek1FridayAM_OUT").on("keydown keyup", function () {
            sum();
        });
    });

    function sum() {
        var num1 = document.getElementById('#txtWeek1FridayAM_IN').value;
        var num2 = document.getElementById('#txtWeek1FridayAM_OUT').value;
        var result = parseFloat(num2) - parseFloat(num1);

        if (!isNaN(result)) {
            document.getElementById('#lblWeek1FridayTotalHrs').value = result;

        }

    }

最佳答案

使用此脚本

$(document).ready(function () {
    sum();
    $('[id$="txtWeek1FridayAM_IN"], [id$="txtWeek1FridayAM_OUT"]').on("keyup", function () {
        sum();
    });
});

function sum() {
    var num1 = $('[id$="txtWeek1FridayAM_IN"]').val();
    var num2 = $('[id$="txtWeek1FridayAM_OUT"]').val();
    var result = parseFloat(num2) - parseFloat(num1);

    if (!isNaN(result)) {
        $('[id$="lblWeek1FridayTotalHrs"]').html(result);

    }

}

关于javascript - 如何减去asp文本框值并将结果放入asp标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51074763/

10-11 06:38