【问题标题】:how to call javascript function?如何调用javascript函数?
【发布时间】:2012-01-31 03:54:42
【问题描述】:

我有一个 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>

【问题讨论】:

    标签: javascript asp.net vb.net gridview


    【解决方案1】:

    只需在页面加载时触发事件,该事件将执行所需的代码并设置标签。

       $(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");
       });
    

    【讨论】:

    • 但是这个 javascript 在 aspx 中,如何在页面加载中放入?我把触发器放在它不起作用,所以
    • 将其包裹在 $(document).ready(function(){}) 中。检查我编辑的答案。
    【解决方案2】:

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

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

    或者,如果您的更改和 keyup 绑定在您的 load 或 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
    

    【讨论】:

      猜你喜欢
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-07
      • 2018-08-15
      • 2012-07-04
      • 2020-09-27
      • 2014-03-23
      相关资源
      最近更新 更多