2013-07-10 50 views
0

UPDATE如何使用.live來停止jQuery .remove以刷新頁面?

我使用jQuery 1.10.1 ...所以.live折舊...我剛剛更換

$("table.dynatable button.remove").live('click', function (e) { 
$(this).parents("tr").remove(); 
}); 

$(document).on("click", "table.dynatable button.remove", function() { 
$(this).parents("tr").remove(); 
}); 

而現在它的工作原理,希望這有助於某人。


我有這樣的jQuery的使用PHP和HTML表單的工作:

我的PHP:

foreach ($matric_type as $row) { 
$matric_type = $row->matric_type; 
} 

該填充取決於基質(12級)認證的類型下拉你有。每個基本認證都有一個不同主題的列表,在另一個下拉列表中填充。

我的jQuery:

$(document).ready(function() { 

    var id = 0; 
    event.preventDefault(); 

    // Add button functionality 
    $("table.dynatable button.add").click(function (e) { 
     e.preventDefault(); 
     id++; 
     var master = $(this).parents("table.dynatable"); 

     // Get a new row based on the prototype row 
     var prot = master.find(".prototype").clone(); 
     prot.attr("class", "") 
     prot.find(".id").attr("value", id); 

     master.find("tbody").append(prot); 
    }); 

    // Remove button functionality 
    $("table.dynatable button.remove").live('click', function (e) { // this might be the problem? 
     //e.preventDefault();   //this does not work 
     // e.stopImmediatePropagation(); //this does not work 
     // e.stopPropagation();   //this does not work 
     $(this).parents("tr").remove(); 
     //$(this).closest('.prototype').remove() 
     // $(this).parent().parent().remove();   
    }); 
}); 

HTML:

<table class="dynatable"> 
          <thead> 
           <tr> 
            <th>Subject No</th> 
            <th>Subject</th> 
            <th>Mark</th> 
            <th><button class="add">Add</button></th> 
           </tr> 
          </thead> 
          <tbody> 
           <tr class="prototype"> 
            <td><input type="input" name="id[]" value="0" class="id" readonly /></td> 
            <td><select id="list"></select></td> 
            <td><input type="text" name="mark[]" value="" /></td> 
            <td><button class="remove">Remove</button> 
           </tr> 
         </table> 

這jQuery使用添加按鈕添加一行與主題人數,科目下拉列表,文本字段爲該主題輸入標記。我可以添加就好了..這個問題是與刪除按鈕..每次我點擊刪除按鈕就刷新頁面,並增加了

?ID%5B%5D = 0 &標記%5B%5D = & ID%5B%5D = 1個&標記%5B%5D =

到URL。更多的「主題」,我添加更長的上述得到例如。如果我添加2名受試者

?編號%5B%5D = 0 &標記%5B%5D = & ID%5B%5D = 1 &標記%5B%5D = & ID%5B%5D = 2 &標記%5B%5D =

我曾嘗試增加了刪除功能:

e.preventDefault();   
e.stopImmediatePropagation(); 
e.stopPropagation(); 

但它仍然重新加載頁面,刪除所有主題行,而不是僅僅被點擊的一個。 有人可以通過給我可能的解決方案來解決我的問題嗎?

+0

當你點擊刪除按鈕是否有被報告任何JS錯誤? – Pattle

+0

你的td包裝按鈕沒有關閉:'​​ ...'。錯字?或者也許相關? – karim79

+0

@Pattle no JS error – Renier

回答

1

添加type=button到您的按鈕,否則按鈕的作用就像一個submit按鈕

<button class="remove" type='button'>Remove</button> 
+0

它幫助重裝問題謝謝,但現在刪除按鈕沒有功能.. – Renier

+0

非常感謝它現在的作品:) – Renier

+0

我使用jQuery 1.10.1 ...所以.live被折舊...我剛剛取代' $(「table.dynatable button.remove」)。live('click',function(e){(this).parents(「tr」)。remove();'with'$(document).on(' 「click」,「table.dynatable button.remove」,function(){ $(this).parents(「tr」)。remove(); });'' – Renier