简体   繁体   English

将html字符串附加为表格行( <tr> )通过javascript-jquery

[英]Append html string as a table row (<tr>) via javascript-jquery

I'm trying to append an HTML string to 我正在尝试将HTML字符串附加到

var field = '<input type="text" name="featureName" class="form-control" id="featureName" placeholder="Feature Name" value="">'
var jfield = $(field);
$('#featureContainer').append(jfield);

When the button is clicked it will crete a input field, but if I click again it creates the input in the same row. 单击该按钮时,它将创建一个输入字段,但如果我再次单击它会在同一行中创建输入。 How can I make a new row with the input in it? 如何使用输入创建一个新行?

The following is my HTML code 以下是我的HTML代码

<tr>
    <td id="featureContainer"></td>
</tr>

If I click the button for the second time it creates it in the same row. 如果我第二次单击该按钮,则会在同一行中创建它。 I want it to create it in new row. 我希望它在新行中创建它。

As we don't know wether tr s are wrapped inside table or tbody , .... We have to look for the closest tr and then get its parent then append a new row to that parent. 因为我们不知道做阉tr s的包裹在里面tabletbody ,......我们必须寻找最接近的tr ,然后再获得其母公司新行追加到父。

So, you should replace this: 所以,你应该替换这个:

$('#featureContainer').append(jfield);

with: 有:

$('#featureContainer').closest('tr').parent().append('<tr><td>' + field + '</td></tr>');

NOTE: that inside field you have a static ID which will be on all the inputs you spawn which will be wrong since IDs are unique. 注意:在内部field您有一个静态ID,它将在您生成的所有输入上,这将是错误的,因为ID是唯一的。 So you may want to assign diferent IDs for diferent inputs. 因此,您可能希望为不同的输入分配不同的ID。

You can just add the html code into field variable, like below: 您只需将html代码添加到field变量中,如下所示:

var field = "<tr><td id="featureContainer"><input type="text" name="featureName" class="form-control" id="featureName" placeholder="Feature Name" value=""></td>
</tr>"
var jfield = $(field);

Assuming there is a button with id = 'add' and a table with id='data' , then you can add this after above code: 假设有一个id = 'add'的按钮和一个id='data'的表,那么你可以在上面的代码之后添加:

$('#add').click(function(){
    $('#data').append(jfield);
});

Your on the right track. 你走在正确的轨道上。 But you don't need the jfield. 但你不需要jfield。

this appends the value of 'field' inside the td element: 这会在td元素中附加'field'的值:

$('#featureContainer').append(field);

but what you want is to append inside the table. 但你想要的是在表内附加。 So give your table a id (or the tbody) and do the following: 所以给你的表一个id (或tbody)并执行以下操作:

You need to embed the field inside a <tr><td> section and append that as a whole. 您需要将该字段嵌入<tr><td>部分并将其作为一个整体附加。

var field = var field = '<tr><td><input type="text" name="featureName" class="form-control" id="featureName" placeholder="Feature Name" value=""></td></tr>';

then in the click event: 然后在点击事件中:

$('#tableid').append(field);

First the id should be unique in the same document so better to use a common classes instead, then you could use append() to add new row (including tr/td ), check the example below. 首先id应该在同一个文档中是唯一的,所以最好使用公共类,然后你可以使用append()来添加新行(包括tr/td ),查看下面的例子。

Hope this helps. 希望这可以帮助。

 $('#add-row').on('click', function(){ var field = '<input type="text" name="featureName" class="form-control" placeholder="Feature Name" value="">' $('table').append('<tr><td>'+field+'</td></tr>'); console.log($('table tr').length+' rows'); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>Default row</td> </tr> </table> <button id='add-row'>Add row</button> 

Thr issue with your code is that you are trying to append to an element using id selector. 您的代码的问题是您尝试使用id选择器追加到元素。 Since in a valid html there should be only a single element with an unique id, you will be appending the new element always to the same td#featureContainer . 由于在有效的html中应该只有一个具有唯一id的元素,因此您将把新元素始终添加到同一个td#featureContainer中

I will suggest you to change the id to class. 我建议你把id更改为class。 To select the td.featureContainer where you need to append the new element, you can check inside the clicked button element event handler and find the td.featureContainer 要选择需要附加新元素的td.featureContainer ,可以在单击的按钮元素事件处理程序中查看并找到td.featureContainer

 $(".feature").on("click", function() { var field = '<input type="text" name="featureName" class="form-control" id="featureName" placeholder="Feature Name" value="">' var jfield = $(field); $(this).parent().prev(".featureContainer").append(jfield); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <table> <tr> <td class="featureContainer"></td> <td> <input type="button" class="feature" value="click for row one"> </td> </tr> <tr> <td class="featureContainer"></td> <td> <input type="button" class="feature" value="click for row two"> </td> </tr> </table> 

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

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