简体   繁体   English

多次追加?

[英]Append multiple times?

I know this a basic question but I don't know what to do.我知道这是一个基本问题,但我不知道该怎么做。 I have been stuck on this from hours but I don't know what is wrong with it.我从几个小时就被困在这个问题上,但我不知道它有什么问题。

what I want to do: I want to append input field when user click on button(simple!).我想要做什么:我想在用户单击按钮时附加输入字段(简单!)。

Problem: It only append one time only.问题:它只追加一次。

This is my fiddle这是我的小提琴

Html code: html代码:

 <table class="table table-bordered" id="tab_logic">
   <thead>
     <tr>
       <th class="text-center">#</th>
       <th class="text-center">Name</th>
       <th class="text-center">Quantity</th>
     </tr>
   </thead>
   <tbody>
     <tr id='addr0'></tr>
   </tbody>
  </table>
  <button id='add' class="btn btn-outline btn-default">Add</button>

Jquery code:查询代码:

var i=0;
    $('#add').click( function() {
      $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
      $('#addr'+(i+1)).html("<td>"+ (i+1) +"</td>"+
      "<td><input type='text' class='form-control input-md' /></td>"+
      "<td><input type='text'  class='form-control input-md'></td>"+
      "<td><a class='btn btn-default glyphicon glyphicon-remove' onclick='deleterow(this)' class'delete'></a></td>" 
      );  
    });
    
      function deleterow(obj){
        $(obj).parent().parent().remove();
        return false;
      }

Sorry I know Its very basic question but my JavaScript is weak.抱歉,我知道这是非常基本的问题,但我的 JavaScript 很弱。

You forgot to increment variable i :你忘了增加变量i

i++

https://jsfiddle.net/pUeue/1781/ https://jsfiddle.net/pUeue/1781/

https://jsfiddle.net/pUeue/1777/ https://jsfiddle.net/pUeue/1777/

var i=0;
    $('#add').click( function() {
      $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
      $('#addr'+(i+1)).html("<td>"+ (i+1) +"</td>"+
      "<td><input type='text' class='form-control input-md' /></td>"+
      "<td><input type='text'  class='form-control input-md'></td>"+
      "<td><a class='btn btn-default glyphicon glyphicon-remove' onclick='deleterow(this)' class'delete'></a></td>");  
      i++;
});

You don't increment i after you click单击后不增加 i

Change your javascript a little bit to work:稍微改变你的 javascript 来工作:

var i=0;
    $('#add').click( function() {
      i += 1;
      $('#tab_logic').append('<tr id="addr'+(i)+'"></tr>');
      $('#addr'+(i)).html("<td>"+ (i) +"</td>"+
      "<td><input type='text' class='form-control input-md' /></td>"+
      "<td><input type='text'  class='form-control input-md'></td>"+
      "<td><a class='btn btn-default glyphicon glyphicon-remove' onclick='deleterow(this)' class'delete'></a></td>" 
      );  
    });

      function deleterow(obj){
        $(obj).parent().parent().remove();
        return false;
      }

variable i is defined as global variable, while in your method, you are not increasing its value.变量i被定义为全局变量,而在您的方法中,您没有增加其值。 That was the issue.这就是问题所在。

Here is the update fiddle: https://jsfiddle.net/pUeue/1784/这是更新小提琴: https : //jsfiddle.net/pUeue/1784/

everytime i value is 0 and i+1 is always 1. add i=i+1;每次 i 值为 0 且 i+1 始终为 1。添加i=i+1; to your code.到您的代码。

Please find below code请找到下面的代码

$('#add').click( function() {debugger;
  $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
  $('#addr'+(i+1)).html("<td>"+ (i+1) +"</td>"+
  "<td><input type='text' class='form-control input-md' /></td>"+
  "<td><input type='text'  class='form-control input-md'></td>"+
  "<td><a class='btn btn-default glyphicon glyphicon-remove' onclick='deleterow(this)' class'delete'></a></td>" 
  );
  i=i+1;                           
});

I realise there are already 4 answers, but I would like to demonstrate the tips I mentioned in comment for creating simple code that is easier to maintain.我意识到已经有 4 个答案,但我想演示我在评论中提到的创建更易于维护的简单代码的技巧。

Also your HTML concatenation is actually not quite right, but nobody can tell with all the string operations.此外,您的 HTML 连接实际上不太正确,但没有人能分辨出所有的字符串操作。

  • Use the existing row count (or last id) etc to determine the next id number to generate.使用现有的行数(或最后一个 id)等来确定要生成的下一个 id 号。
  • Consider using a dummy/template element instead of HTML string concatenation.考虑使用虚拟/模板元素而不是 HTML 字符串连接。 This allows for maintainable templates.这允许可维护的模板。
  • Use single quotes for the outer strings, that that the HTML attributes have double-quotes (for browser compatibility).对外部字符串使用单引号,即 HTML 属性具有双引号(为了浏览器兼容性)。

This example has the first two as the quotes issue goes away:这个例子有前两个,因为引号问题消失了:

JSFiddle: https://jsfiddle.net/pUeue/1787/ JSFiddle: https ://jsfiddle.net/pUeue/1787/

$('#add').click(function () {
    var count = $('#tab_logic tr').length;
    $('#tab_logic').append($('#template').html().replace(/{i}/g, count));
});

function deleterow(obj) {
    $(obj).parent().parent().remove();
    return false;
}

In this example your template row sits in a dummy script block (of unknown type so is ignored).在此示例中,您的模板行位于一个虚拟脚本块(未知类型,因此被忽略)中。 It uses a "global" replace option in a regex to replace all occurrences of a placeholder with the desired value(s).它在正则表达式中使用“全局”替换选项来用所需的值替换所有出现的占位符。

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

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