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

lit*_*326 2 javascript jquery function click

我有一个用户可以选择项目的网站,然后显示详细信息,包括数量.我还在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);
});
Run Code Online (Sandbox Code Playgroud)

我发出警告消息,看看按钮是否正常工作,但是当我点击btnRemove1按钮时,没有任何反应.

tym*_*eJV 8

由于该按钮是动态添加的,您可以尝试:

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