jQuery功能失调,Chrome控制台中没有任何提示

时间:2018-06-20 05:28:47

标签: javascript jquery asp.net

我具有打算用来禁用满足某些条件的按钮的功能。

<script type="text/javascript" src="Scripts/jquery-1.7.1.min.js"></script>

     <script type ="text/javascript">
         $(document).ready(function () {

             //TEXT BOX 1
         $('#<%=txtAmount_VH.ClientID%>').keyup(function (e) {

             $vhamnt = $('#<%=txtAmount_VH.ClientID%>').val();
             $mxamnt = $('#<%=txtAmount_MX.ClientID%>').val();
             $obamnt = $('#<%=txtAmount_OB.ClientID%>').val();
             $spamnt = $('#<%=txtAmount_SP.ClientID%>').val();

             $totamnt = $('#<%=txtTotalSecurityDeposit.ClientID%>').val();
             $sum = $vhamnt + $mxamnt + $obamnt + $spamnt;
             if ($sum <= $totamnt) {
                 $('#<%=btnAddAmount.ClientID %>').attr("disabled", "disabled");
             }
         });
             //TEXT BOX 2
             $('#<%=txtAmount_MX.ClientID%>').keyup(function (e) {

             $vhamnt = $('#<%=txtAmount_VH.ClientID%>').val();
             $mxamnt = $('#<%=txtAmount_MX.ClientID%>').val();
             $obamnt = $('#<%=txtAmount_OB.ClientID%>').val();
             $spamnt = $('#<%=txtAmount_SP.ClientID%>').val();

             $totamnt = $('#<%=txtTotalSecurityDeposit.ClientID%>').val();
             $sum = $vhamnt + $mxamnt + $obamnt + $spamnt;
             if ($sum <= $totamnt) {
                 $('#<%=btnAddAmount.ClientID %>').attr("disabled", "disabled");
             }
         });

             //TEXT BOX 3
             $('#<%=txtAmount_OB.ClientID%>').keyup(function (e) {

             $vhamnt = $('#<%=txtAmount_VH.ClientID%>').val();
             $mxamnt = $('#<%=txtAmount_MX.ClientID%>').val();
             $obamnt = $('#<%=txtAmount_OB.ClientID%>').val();
             $spamnt = $('#<%=txtAmount_SP.ClientID%>').val();

             $totamnt = $('#<%=txtTotalSecurityDeposit.ClientID%>').val();
             $sum = $vhamnt + $mxamnt + $obamnt + $spamnt;
             if ($sum <= $totamnt) {
                 $('#<%=btnAddAmount.ClientID %>').attr("disabled", "disabled");
             }
         });

             //TEXT BOX 4
             $('#<%=txtAmount_SP.ClientID%>').keyup(function (e) {

             $vhamnt = $('#<%=txtAmount_VH.ClientID%>').val();
             $mxamnt = $('#<%=txtAmount_MX.ClientID%>').val();
             $obamnt = $('#<%=txtAmount_OB.ClientID%>').val();
             $spamnt = $('#<%=txtAmount_SP.ClientID%>').val();

             $totamnt = $('#<%=txtTotalSecurityDeposit.ClientID%>').val();
             $sum = $vhamnt + $mxamnt + $obamnt + $spamnt;
             if ($sum <= $totamnt) {
                 $('#<%=btnAddAmount.ClientID %>').attr("disabled", "disabled");
             }
         });
        </script>

此外,控制台并没有太大帮助。

  

我希望该函数在每次的值总和时禁用按钮   文本框大于另一个特定的文本框。但是,那是   完全没有发生。

我错过了明显的东西吗?还是更多?

1 个答案:

答案 0 :(得分:1)

您应该使用parseInt来实现正确的操作,如下所示:

if (parseInt($sum) <= parseInt($totamnt)) {
    $('#<%=btnAddAmount.ClientID %>').attr("disabled", "disabled");
}

修改

您可以用相同的方式检查所有输入。

$('input').keyup(function(e) {

    $vhamnt = $('#txtAmount_VH').val();
    $mxamnt = $('#txtAmount_MX').val();
    $obamnt = $('#txtAmount_OB').val();
    $spamnt = $('#txtAmount_SP').val();

    $totamnt = $('#txtTotalSecurityDeposit').val();
    $sum = $vhamnt + $mxamnt + $obamnt + $spamnt;
    if (parseInt($sum) <= parseInt($totamnt)) {
        $('#btnAddAmount').attr("disabled", "disabled");
    }
});

Online demo (jsFiddle)

编辑1

这是我检查过的测试标记,对我来说正常工作

<div>
    <asp:TextBox Class="checkAmount" ID="txtAmount_VH" runat="server" />
    <asp:TextBox Class="checkAmount" ID="txtAmount_MX" runat="server" />
    <asp:TextBox Class="checkAmount" ID="txtAmount_OB" runat="server" />
    <asp:TextBox Class="checkAmount" ID="txtAmount_SP" runat="server" />
    <asp:TextBox Class="checkAmount" ID="txtTotalSecurityDeposit" runat="server" />
    <asp:Button ID="btnAddAmount" Text="AddAmount" runat="server" />
</div>

<script>
    $(document).ready(function() {

        $('.checkAmount').keyup(function(e) {

            $vhamnt = $('#txtAmount_VH').val();
            $mxamnt = $('#txtAmount_MX').val();
            $obamnt = $('#txtAmount_OB').val();
            $spamnt = $('#txtAmount_SP').val();

            $totamnt = $('#txtTotalSecurityDeposit').val();
            $sum = $vhamnt + $mxamnt + $obamnt + $spamnt;
            if (parseInt($sum) <= parseInt($totamnt)) {
                $('#btnAddAmount').attr("disabled", "disabled");
            }
        });

    });
</script>