2013-03-27 164 views
1

我有一个网站,用户可以选择一个项目,细节然后显示,包括数量。我也包括在div内的按钮,以便单击时,它应该由1JavaScript的按一下按钮功能不能正常工作

 $("#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); 
}); 

我把一条警告消息减少量,看看是否按钮是否工作正常,但是当我点击btnRemove1按钮,没有任何反应。

+0

哪里是你的HTML代码加载?你使用的是什么版本的jQuery? – DaveHogan 2013-03-27 17:41:02

+1

你不应该在这里通知一个字符串吗? 'alert(remove);' – 2013-03-27 17:41:12

+0

你可以在你创建btnRemove1的地方添加代码吗? btnBuy1点击功能是否按预期工作? – FloatingCoder 2013-03-27 17:43:23

回答

7

由于按钮是动态添加,你可以尝试:

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

尝试把删除按钮单击处理程序此外,您创建的删除按钮

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

您可以扳平事件后该文件(jQuery生活之前曾经做过的事情)

现在它是:

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

或#btnRemove1被添加到DOM后,你可以重新绑定事件。

0

这是因为按钮后(动态地)加入。你将不得不使用委托。

你不必用身体这一点。任何非动态插入的元素都是#btnRemove1的父元素。

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

原因是您在页面上存在元素#btnRemove1之前绑定事件。因此没有什么可以将事件绑定到。然而,body元素将出现在页面上并将您的活动委托给#btnRemove1

1

最有可能的,你的删除按钮是不是在DOM尝试点击事件附加到它。很难从代码片断中知道,但如果“购买”按钮操作未成功完成,则Remove将不存在。看到

在所附加的单击事件删除,尝试控制台记录$(“#btnRemove1”)的点。长度(如果存在),或者使用断点。

你的代码的改进将是一个变量$缓存(“#投寄箱”),然后在其中寻找你的按钮,如:

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

而且你应该使用。对()而不是已弃用的.click()。

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

不工作如果数据与AJAX在DIV区 恰似

<a href="javascript:;;" class="edit">EDIT</a> 
相关问题