2012-01-31 10 views
0

iは、負荷のdefault.aspxは、txtQualityは、データベーステーブルからのカウント値を取得する際にどのようにJavaScript関数を呼び出すには?

<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> 

しかし品質で価格を取ることによって、トータルコストを計算するのに役立つJavaスクリプト機能を持っていますしかし、テーブル値だけが変更されている場合、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> 

答えて

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"); 
    }); 
+0

このJavaScriptはaspxにありますが、どのようにページの読み込みに入れますか?私はそれがうまくいかないので、 – actKing

+0

何が間違っている? – actKing

+0

'$(document).ready(function(){})'の中にラップします。私の編集された答えをチェックしてください。 – ShankarSangoli

0

あなたのdocument.readyハンドラ内から、あなたのonloadハンドラからイベントを.trigger()、または、より良いことができます。

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

またはあなたの変化とからkeyupバインディングは、あなたの負荷や文書で行われているかを

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

    // and the same for keyup 
}); 

それともトンので、あなたのイベントハンドラのために無名関数を使用しないようにコードを変更することができます.readyとにかくあなたは.trigger()呼び出しを単に連鎖することができます他の場所から関数を直接呼び出すことができます:

// 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 
関連する問題