2012-01-31 136 views
0

我有一個Java腳本功能,幫助由質量如何調用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> 

然而,當負載的Default.aspx,該txtQuality會從數據庫中檢索表計數值採取價格*計算總成本,但如何才能工作,如果只有表值是變化,但除此之外,我還希望有一個結果,當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 2012-01-31 04:51:28

+0

所以有什麼錯? – actKing 2012-01-31 04:55:50

+0

將它包裝在'$(document).ready(function(){})'中。檢查我編輯的答案。 – ShankarSangoli 2012-01-31 14:58:29

0

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

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

或者,如果你的變化和KEYUP綁定在你的負載或文檔完成。就緒反正你可以鏈.trigger()電話:

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

    // and the same for keyup 
}); 

或者你可以改變你的代碼不使用匿名函數爲您的事件處理程序因此T帽子,你可以直接從其他地方調用該功能:

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