2013-03-27 119 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>