简体   繁体   English

javascript按钮单击功能无法正常工作

[英]javascript button click function not working

I have a website where user can select an item, the detail is then displayed, including the quantity. 我有一个用户可以选择项目的网站,然后显示详细信息,包括数量。 I have also included a button inside the div so that when clicked, it should decrease the quantity by 1. 我还在div中包含了一个按钮,这样当单击时,它应该将数量减少1。

        $("#btnBuy1").click(function()
        {
            if (!sessionStorage['quantity1'])
            {
                sessionStorage['quantity1']=1;

            }
            else
            {
                sessionStorage['quantity1']++;
            }
            $("#dropbox").html('<div id = "1"><img class = "thumb" id = "t1" src="../images/birthday_metoyou.jpg" />' + teddy[1].desc + ", Price £"
             + teddy[1].price + ", Quantity: " + sessionStorage.getItem('quantity1') + "<button id = 'btnRemove1'>Remove</button></div><br/>");
            updateBasket();
            sessionStorage["total1"] = parseInt(sessionStorage.getItem('quantity1')) * teddy[1].price;
            updateSubtotal();
            if (Modernizr.sessionstorage) 
            {  // check if the browser supports sessionStorage
                myids.push(teddy[1].partnum); // add the current username to the myids array
                sessionStorage["ids"]=JSON.stringify(myids); // convert it to a string and put into sessionStorage
            } 
            else 
            {
             // use cookies instead of sessionStorage
            }            
        });

$("#btnRemove1").click(function()
{
   alert(remove);
});

I put in an alert message to see if the button is working properly, but when I click the btnRemove1 button, nothing happens. 我发出警告消息,看看按钮是否正常工作,但是当我点击btnRemove1按钮时,没有任何反应。

Since the button is dynamically added, can you try: 由于该按钮是动态添加的,您可以尝试:

$(document).on('click', '#btnRemove1', function() {
{
   alert("remove"); //I dont know what remove was is the example, added quotes around it.
});

You can either tie the event to the document (what jQuery live used to do before it was deprecated) 您可以将事件绑定到文档(jQuery在弃用之前曾经做过的事情)

now it is: 现在它是:

$(document).on("click", "#btnRemove1", function(){})

or you can rebind the event after #btnRemove1 is added to the Dom. 或者你可以在#btnRemove1添加到Dom之后重新绑定事件。

Most likely, your Remove button isn't in the DOM before you try to attach the click event to it. 最有可能的是,在尝试将click事件附加到DOM之前,“删除”按钮不在DOM中。 It is hard to tell from your code snippets, but if the Buy button action hasn't completed successfully, then Remove won't exist. 很难从您的代码片段中分辨出来,但如果“购买”按钮操作未成功完成,则“删除”将不存在。

At the point that you attach the click event to Remove, try console logging $("#btnRemove1").length to see if it exists, or use break points. 在将click事件附加到Remove时,请尝试控制台记录$(“#btnRemove1”)。length以查看它是否存在,或使用断点。

An improvement to your code would be to cache in a variable $("#dropbox") and then look for your buttons within it, as in: 对代码的改进是缓存在变量$(“#dropbox”)中,然后在其中查找按钮,如:

var $dropBoxNode = $("#dropbox");
$dropBoxNode.find("#btnRemove1");

And you should use .on() instead of the deprecated .click(). 你应该使用.on()而不是弃用的.click()。

Try putting the remove button click handler addition after you create the remove button 创建删除按钮后,尝试将删除按钮单击处理程序添加

///Code snippit

$("#dropbox").html('<div id = "1"><img class = "thumb" id = "t1" src="../images/birthday_metoyou.jpg" />' + teddy[1].desc + ", Price £"
                     + teddy[1].price + ", Quantity: " + sessionStorage.getItem('quantity1') + "<button id = 'btnRemove1'>Remove</button></div><br/>");

$("#btnRemove1").click(function()
{
     alert(remove);
});

updateBasket();

///Code snippit

That is because the button is added later (dynamicly). 这是因为稍后(动态地)添加按钮。 You will have to use a delegate. 您将不得不使用委托。

You don't have to use body for this. 你不必为此使用身体。 Any non dynamicly inserted element that is a parent of #btnRemove1 will do. 任何非动态插入的元素都是#btnRemove1的父元素。

$('body').on('click','#btnRemove1',function(){
   alert(remove);
});

The reason is that you bind the event before the element #btnRemove1 is present on your page. 原因是您在页面上存在元素#btnRemove1之前绑定事件。 Therefore there is nothing to bind the event to. 因此没有什么可以将事件绑定到。 The body element however - will be present on the page and delegate your event to #btnRemove1 . 然而,body元素将出现在页面上并将您的事件委托给#btnRemove1

$('a.edit').on('click','a.edit',function(){
   if (confirm("Are you sure you want to Edit this Photo?"))
   {
    ....
    ....            
   }
});

Not Working When Data is Loading with AJAX on a DIV Area Just Like 在DIV区域上使用AJAX加载数据时不起作用

<a href="javascript:;;" class="edit">EDIT</a>

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

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