繁体   English   中英

如何调用javascript函数?

[英]how to call javascript function?

我有一个Java脚本功能,可以通过按价格*质量来帮助计算总成本

<script type="text/javascript">

               $("[id*=txtQuality]").live("change", function () {
                   if (isNaN(parseInt($(this).val()))) {
                       $(this).val('0');
                   } else {
                       $(this).val(parseInt($(this).val()).toString());
                   }
               });
               $("[id*=txtQuality]").live("keyup", function () {
                   if (!jQuery.trim($(this).val()) == '') {
                       if (!isNaN(parseFloat($(this).val()))) {
                           var row = $(this).closest("table");
                           $("[id*=lblTotal]", row).html(parseFloat($("[id*=price]", row).html()) * parseFloat($(this).val()));
                       }
                   } else {
                       $(this).val('');
                   }

               });

</script>

但是,当加载default.aspx时,txtQuality将从数据库表中检索一个计数值,但是如果仅更改表值,javascript将如何工作,但是除此之外,我还希望得到一个结果,当default.aspx为加载时,“ lblTotal”具有使用计数值*价格由javascript计算的金额

 Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then


            Dim txt As TextBox = DirectCast(e.Row.FindControl("txtQuality"), TextBox)
            Dim adapter As New SqlDataAdapter
            Dim ds As New DataSet
            'Dim sql As String

            Dim connectionString = ConfigurationManager.ConnectionStrings("ProjData").ConnectionString
            Dim myConn As New SqlConnection(connectionString)

            Dim cmd = "Select * From Product Where customerID='" & Session("customerID") & "' "

            ' Dim myCmd As New SqlCommand(cmd, myConn)

            Try
                myConn.Open()
                Dim myCmd As New SqlCommand(cmd, myConn)
                adapter.SelectCommand = myCmd
                adapter.Fill(ds, "Product")
                adapter.Dispose()
                myCmd.Dispose()
                txt.Text = ds.Tables(0).Rows.Count



            Catch ex As Exception
                MsgBox("Can not open connection ! ")
            End Try
        End If
    End Sub

 <table style="width: 79%; height: 31px;">
                    <tr>
                        <td class="style1">
                            <asp:Label ID="Label7" runat="server" Text="price    $"></asp:Label>
                        </td>
                        <td >
                            <asp:Label ID="price" runat="server" Text='<%# Bind("costPerTable") %>' ></asp:Label>

                    </td>
                </tr>
                <tr>
                    <td class="style1">
                        <asp:Label ID="Label2" runat="server" Text="Quantity"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
                    </td>
                </tr>

                <tr>
                    <td class="style1">
                        <asp:Label ID="Label8" runat="server" Text="Total Cost:"></asp:Label>
                    </td>
                    <td>
                        $<asp:Label ID="lblTotal" runat="server" ></asp:Label>
                    </td>
                </tr>

            </table>
        </ItemTemplate>

    </asp:TemplateField>

只需在页面加载时触发事件即可执行所需代码并设置标签。

   $(document).ready(function(){
           $("[id*=txtQuality]").live("change", function () {
               if (isNaN(parseInt($(this).val()))) {
                   $(this).val('0');
               } else {
                   $(this).val(parseInt($(this).val()).toString());
               }
           }).trigger("change");

           $("[id*=txtQuality]").live("keyup", function () {
               if (!jQuery.trim($(this).val()) == '') {
                   if (!isNaN(parseFloat($(this).val()))) {
                       var row = $(this).closest("table");
                       $("[id*=lblTotal]", row).html(parseFloat($("[id*=price]", row).html()) * parseFloat($(this).val()));
                   }
               } else {
                   $(this).val('');
               }

           }).trigger("keyup");
   });

您可以从onload处理程序中,或者更好地,从document.ready处理程序中, .trigger()事件:

$(document).ready(function() {
   $("[id*=txtQuality]").trigger("change")
                        .trigger("keyup");
});

或者,如果您的更改和键盘绑定是在您的负载或document.ready中完成的,则无论如何您都可以链接.trigger()调用:

$(document).ready(function() {
   $("[id*=txtQuality]").live("change", function() {
        // your function body here
   }).trigger("change");

   // and the same for keyup
});

或者,您可以更改代码以不将匿名函数用于事件处理程序,以便可以直接从其他位置调用该函数:

// declare function to be called on change
function myChangeHandler() {
   // your existing change handler code here
}

// bind event to function
$(("[id*=txtQuality]").live("change", myChangeHandler);

// then you can call the function from other places, including onload:
myChangeHandler();

// and the same for your keyup

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM