简体   繁体   English

为什么jquery click事件无法触发?

[英]Why jquery click event not firing?

  1. default.aspx click on the + symbol to increase the quantity. default.aspx单击+符号以增加数量。 and - symbol for decrease the quantity. 和-用于减少数量的符号。

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

    In this when I click on the + symbol no event firing.How do i do with that jquery? 在这种情况下,当我单击+符号时不会触发任何事件。如何处理该jquery?

You need to wrap your code inside $(document).ready() : 您需要将代码包装在$(document).ready()

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

Add your jQuery stuff, inside this function 在此函数内添加您的jQuery内容

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

keep your jquery code inside head section its just counts in best practices and one thing more add this code in 将您的jquery代码保留在头部,这仅是最佳做法,还有一点要在此代码中添加

$(document).ready(function(){

 });

this will attach the events to the elements of given class when you document is ready and fully loaded/Dom is ready. 当您准备好文档并且已完全加载/ Dom准备就绪时,这会将事件附加到给定类的元素上。

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

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