简体   繁体   English

jQuery的克隆按钮动作不起作用

[英]jquery cloned button action not working

i have a table with add/remove buttons, those buttons add and remove rows from the table, the buttons are also added with each new row 我有一个带有添加/删除按钮的表,这些按钮添加和删除表中的行,每个新行也添加按钮

here what i have as html 这是我拥有的HTML

<table>
<tr>
   <th>catalogue</th>
   <th>date</th>
   <th>add</th>
   <th>remove</th>
</tr>
<- target row ->
<tr id="cat_row">
   <td>something</td>
   <td>something</td>
   <td><input id="Add" type="button" value="Add" /></td>
   <td><input id="Remove" type="button" value="Remove" /></td>
</tr>
</- target row ->
</table>

JavaScript: JavaScript:

$("#Add").click(function() {
         $('#cat_row').after('<- target row with ->'); // this is only a notation to prevent repeatation
         id++;
});

$("#Remove").click(function() {
         $('#cat_'+id+'_row').remove();
         id--;
});

Please note that after each addation of a new row the id is also changed for example here after clicking the button "Add" 1 time 请注意,每次添加新行后, id也会更改,例如,单击“添加”按钮1次后,此处的id也将更改

<table>
<tr>
   <th>catalogue</th>
   <th>date</th>
   <th>add</th>
   <th>remove</th>
</tr>
<tr id="cat_row">
   <td>something</td>
   <td>something</td>
   <td><input id="Add" type="button" value="Add" /></td>
   <td><input id="Remove" type="button" value="Remove" /></td>
</tr>
<tr id="cat_1_row">
   <td>something</td>
   <td>something</td>
   <td><input id="Add" type="button" value="Add" /></td>
   <td><input id="Remove" type="button" value="Remove" /></td>
</tr>
</table>

now the new added Buttons has no actions i must always click on the original buttons - add/remove 现在, 新添加的按钮没有动作,我必须始终单击原始按钮 -添加/删除

after this i want to make the Remove Button removes ONLY the row where it is clicked on for example if i click the button in row 2, row 2 will be deleted 在此之后,我想使“ 删除按钮” 删除单击它的行,例如,如果我单击第2行中的按钮,则将删除第2行


Info: I use web2py 2.2.1 with python 2.7 with the last version of jQuery 信息:我将python 2.7和web2py 2.2.1与jQuery的最新版本一起使用

You can't have two buttons with the same id (you're using duplicates for both "Add" and "Remove"). 您不能有两个具有相同id按钮(您对“添加”和“删除”都使用了重复的按钮)。 id values must be unique on the page . id值在页面上必须唯一

You might look at using a class instead, and also look at event delegation. 您可能会考虑使用一个类,同时还会考虑事件委托。 For instance, assuming this is the only table on your page (if not, just make the selector more specific), if you change your buttons to have classes "add-btn" and "remove-btn" instead of id values, then you can use delegate : 例如,假设这是页面上的唯一表(如果没有,则只需使选择器更具体),如果将按钮更改为具有“ add-btn”和“ remove-btn”类而不是id值,则您可以使用delegate

$("table").delegate(".add-btn", "click", function() {
    // An add button was pressed, you can find out which
    // row like this:
    var row = $(this).closest('tr');
});

...and similarly for the .remove-btn button. ...以及类似的.remove-btn按钮。

Alternately, some prefer to use on (note the order of arguments is slightly different): 或者,有些用户更喜欢使用on (请注意,参数的顺序略有不同):

$("table").on("click", ".add-btn", function() {
    // An add button was pressed, you can find out which
    // row like this:
    var row = $(this).closest('tr');
});

Which you use is currently a matter of style. 当前使用的是样式问题。 I prefer delegate for clarity, but the jQuery team love to hyper-overload their functions. 为了清晰起见,我更喜欢使用delegate ,但是jQuery团队喜欢超载其功能。 So far they haven't deprecated delegate , though they do call it "superceded." 到目前为止,他们还没有弃用delegate ,尽管他们确实称其为“替代”。

I think it would be better instead of: 我认为这会比以下更好:

$('#cat_'+id+_row').remove();

Do this in your onclick event: 在您的onclick事件中执行此操作:

$(this).parent().parent().remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM