關於jquery的問題,按鈕點擊事件

來源:互聯網
上載者:User
有A、B兩個頁面
A頁面有多個“修改”按鈕,每個按鈕有樣式class="editbtn",點擊ajax到B頁面,如下代碼
$(".editbtn").click(function(){var url="b.php";var params = $('form').serialize();$.ajax({url: url,type: 'post',dataType:'json',data: params,success: function (data){$("#div1").html(data.html);$("#div2").html(data.script);}});});

B頁面
$data=array('html'=>$html,'script'=>$script);echo json_encode($data);

B頁面也會返回N條包含class="editbtn"的“修改”按鈕,返回的這些按鈕點擊就無法完成$(".editbtn").click了,請問是什麼原因呢?
之所以B頁面有'script'=>$script,就是因為無法完成$(".editbtn").click事件,我又在B頁面加了上面那段jquery,把它又弄到$("#div2").html,這樣就可以運行,但是這樣我感覺效率不好,好像如果點擊了很多次就重複了很多次相同的代碼,大神們覺得這樣好嗎?應該不好吧?


回複討論(解決方案)

把 $(".editbtn").click(function(){
改為 $(document).on('click', ".editbtn", function(){
就可以了

把 $(".editbtn").click(function(){
改為 $(document).on('click', ".editbtn", function(){
就可以了


謝謝解答哈。
我之前是放在$(document).ready裡面的,按照你說的我不管是放在這個內還是外提示:
TypeError: $(...).on is not a function

綁定事件改成
$('.clickme').live('click', function() {
alert("Live handler called.");
});
live:給所有匹配的元素附加一個事件處理函數,即使這個元素是以後再添加進來的也有效

把 $(".editbtn").click(function(){
改為 $(document).on('click', ".editbtn", function(){
就可以了


換了jquery版本可以了,之前用的jquery-1.6.2.min,改為jquery-1.8.3.min,謝謝,好多問題都是你回答解決的。
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.