如何使用.live停止函数中的jQuery.remove以刷新页面


How to stop jQuery .remove in function with .live to refresh the page?

更新

我使用的是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个主题

id%5B%5D=0&标记%5B%5D=&id%5B%5D=1&标记%5B%5D=&id%5B%5D=2&标记%5B%5D=

我已经尝试添加到删除功能:

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

但它仍然会重新加载页面,删除所有主题行,而不仅仅是单击的那一行。有人能为我的问题提供一个可能的解决方案吗?

type=button添加到按钮中,否则按钮的作用类似于submit按钮

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