简体   繁体   English

jQuery表追加

[英]Jquery table Append

I need help to understand what I am doing wrong. 我需要帮助以了解我在做什么错。 I have the following table in the loop: 我在循环中有下表:

<table width="100%" class="box-table-green" align="left" id="tableAppend" >
   <thead>
     <tr>
      <th align="left"><?php echo $table_name; ?></th>
      <th   align="Right"> <a href="javascript:void(0);" id="addCF"  >Add Row</a> </th>
     </tr>
   </thead>
</table>

And by click on Add Row, it will generate a row in the respective table with the help of the following Javascript code: 通过单击“添加行”,它将在以下Javascript代码的帮助下在相应的表中生成一行:

$('.box-table-green a').click(function (evt) {
             evt.preventDefault();


    var rowHtml = '<tr  align="center"><td><input type="text"  name="vsname[]" value="" /></td><td><input type="text" name="foreRengoring[]" id="foreRengoring" class="fieldWidthBefore80" /><img src="images/cancel_icon.png" style="float:right" width="24" height="24" class="deleteRowNew" /></td></tr>'; 

      $(this).parents('.box-table-green').append(rowHtml);
     return false;
 });

if I only leave the above Jquery code, then it appends only one row. 如果我只留下上面的Jquery代码,那么它将仅追加一行。 but when I add the delete code, which is below, then it appends two rows and I don't know why. 但是当我添加下面的删除代码时,它会追加两行,我不知道为什么。

$("#tableAppend").on('click', ".deleteRowNew", function () { 
 $(this).closest('tr').remove();
 });

I tried it with class name, and direct click on image as well. 我尝试使用类名进行尝试,并直接单击图像。 incase of direct $("img").click , it only remove the rows that are pre-loaded intot hte page. 如果直接使用$("img").click ,则仅删除预加载到该页面中的行。 And does not work on the rows that are dynmaically created with the above Append Row code. 并且不适用于使用上述“附加行”代码动态创建的行。

Need help, as I have spent a lot of time on this thing, and I'm not able to figure out what am I doing wrong. 需要帮助,因为我花了很多时间在这件事上,而且我无法弄清楚我在做什么错。

Updated fiddle: http://jsfiddle.net/lotusgodkk/GCu2D/12/ 更新的小提琴: http : //jsfiddle.net/lotusgodkk/GCu2D/12/

You had some issues in the html and in the js. 您在html和js中遇到了一些问题。 In HTML you missed the <tr> tag. 在HTML中,您错过了<tr>标记。 In js you were appending the html which had input of same Id. 在js中,您要追加具有相同ID输入的html。 Never use same ID on elements. 切勿在元素上使用相同的ID。 So I removed ID attribute from Input. 所以我从输入中删除了ID属性。 And the code is working as expected. 并且代码按预期工作。

$(document).ready(function () {
$('.box-table-green a').click(function (evt) {
    evt.preventDefault();
    var rowHtml = '<tr  align="center"><td><input type="text"  name="vsname[]" value="" /></td><td><input type="text" name="foreRengoring[]" class="fieldWidthBefore80" /><img src="images/cancel_icon.png" style="float:right" width="24" height="24" class="deleteRowNew" /></td></tr>';
    $(this).parents('.box-table-green').append(rowHtml);
    return false;
});
$("#tableAppend").on('click', ".deleteRowNew", function () {
    $(this).closest('tr').remove();
  });
});

I couldn't figure out why my page was not working, the way it should have. 我不知道为什么我的页面无法正常工作。 But I solved my execution flow by another method. 但是我用另一种方法解决了执行流程。 I guess can not paste the whole page here, and the code (provided here) works fine. 我猜不能在此处粘贴整个页面,并且代码(在此处提供)可以正常工作。

I don't konw how to close this question, Consider this question close. 我不知道如何结束这个问题,请考虑结束这个问题。 As it will be useless for some one to comment on it. 因为有人对此发表评论将毫无用处。

Thanks 谢谢

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

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