簡體   English   中英

為什么jquery click事件無法觸發?

[英]Why jquery click event not firing?

  1. default.aspx單擊+符號以增加數量。 和-用於減少數量的符號。

      <div class="sp-quantity"> <div class="sp-minus fff"> <a class="ddd" href="#">-</a> </div> <div class="sp-input"> <asp:TextBox ID="txtQuantity" runat="server" CssClass="popuptextbox quntity-input" Width="30px" Text='<%#Eval("Quantity")%> '></asp:TextBox> </div> <div class="sp-plus fff"> <a class="ddd" href="#">+</a> </div> <%--<asp:LinkButton ID="lbtnUpdateQuantity" runat="server" CommandName="Update"><i class="fa fa-check-circle" style="color:#00374A"></i></asp:LinkButton>--%> </div> jquery: <script type="text/javascript"> $(".ddd").on("click", function () { var $button = $(this); var oldValue = $button.closest('.sp-quantity').find("#ContentPlaceHolder1_gvItems_txtQuantity").val(); //var oldValue = $("#ContentPlaceHolder1_gvItems_txtQuantity").val(); if ($button.text() == "+") { var newVal = parseFloat(oldValue) + 1; } else { if (oldValue > 0) { var newVal = parseFloat(oldValue) - 1; } else { newVal = 0; } } $button.closest("#ContentPlaceHolder1_gvItems_txtQuantity").val(newVal); }); </script> 

    在這種情況下,當我單擊+符號時不會觸發任何事件。如何處理該jquery?

您需要將代碼包裝在$(document).ready()

 $(document).ready(function(){
      $(".ddd").on("click", function () {
         ...
      })
 });

在此函數內添加您的jQuery內容

$(function() {
    //jQuery stuff
})

將您的jquery代碼保留在頭部,這僅是最佳做法,還有一點要在此代碼中添加

$(document).ready(function(){

 });

當您准備好文檔並且已完全加載/ Dom准備就緒時,這會將事件附加到給定類的元素上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM