简体   繁体   English

通过PHP / jQuery获取动态创建的行的值

[英]Get values of dynamically created row via PHP/jQuery

I needed to get the values of each dynamically created rows and print those values. 我需要获取每个动态创建的行的值并打印这些值。 What the current code does is that it can alert the all the added values or print to console. 当前代码的作用是可以警告所有添加的值或打印到控制台。 Now, how can I print those values? 现在,如何打印这些值?

For instance user has added one more row and selected the following: 例如,用户添加了另一行并选择了以下内容:

在此处输入图片说明

After the user clicks Get Values, all the values (Female, Lady, Male and Schertz) must be printed to a generated table and page should look like this: 用户单击“获取值”后,必须将所有值(女性,女士,男性和谢尔兹​​)打印到生成的表中,页面应如下所示:

在此处输入图片说明

Now what I have are the following code. 现在,我有以下代码。

HTML view: HTML视图:

<button>Add More Order</button>
<button>Delete Added Order</button>

<div id="wrap" class="order-container">
  <p>       
    <select class="getThis">
      <option>--Gender--</option>
      <option value="Male">Male</option>
      <option value="Female">Female</option>
    </select>   
    <input type="text" class="getThis" placeholder="Name"/>
  <br>
  </p>
</div>

  <input type="submit" id="getValues" value="Get Values"></input>

jQuery: jQuery的:

$(document).ready(function(){

    //Add new row   
    $("button:nth-of-type(1)").click(function(){
        $("#wrap").append("<p><select class='getThis'><option>--Gender--</option><option>Male</option><option>Female</option></select><input type='text' class='getThis' placeholder='Name'/><br></p>").trigger('create');
    });

    //Remove last added row
    $("button:nth-of-type(2)").click(function(){
        $("p:last-of-type").remove();
    });

//alert values
$("#getValues").click(function() {
        $(".getThis").each(function() {
           var getThis =  $(this).val();
           //console.log(getThis);
           alert(getThis);

     });

  });

}); // Document Ready End

Here's a JSfiddle for a demo. 这是演示的JSfiddle

Any ideas? 有任何想法吗? Kamsahamnida! Kamsahamnida!

try this: 尝试这个:

 $("#getValues").click(function() {
    $("select.getThis").each(function() {
       var getThis =  $(this).val();
       $(this).next().val(getThis);
 });});

Working Demo 工作演示

尝试

document.getElementById('getValues').value;

Change your inputs class to something else as it might start confilcting and then try this: 将输入类更改为其他类,因为它可能会开始合并,然后尝试以下操作:

$("#getValues").click(function() {
  $(".getThis").each(function() {
    var getThis =  $(this).val();
    $(this).closest("p").find("input[type='text']").val(getThis);
  });
});

What it does is it goes from the current target to the closest parent paragraph tag and from there it finds the input[type='text'] and gives it the value 它的作用是将其从当前目标移到最接近的父段标记,然后从那里找到input [type ='text']并为其赋值

You could use eg: 您可以使用例如:

DEMO DEMO

$("#getValues").click(function () {
        var $table = $('#resultTable').length ? $('#resultTable').find('tr:gt(0)').remove().end() : $('<table/>').append('<tr><th>Gender</th><th>Name</th>').appendTo('body').attr('id', 'resultTable');
        var getValues = $("p:has(select.getThis)").find('select.getThis').map(function () {
            return {
                gender: this.value,
                name: $(this).next().val()
            }
        }).get();
        $.each(getValues, function (_, fields) {
            $table.append($('<tr/>', {
                html: '<td>' + fields.gender + '</td><td>' + fields.name + '</td>'
            }))
        });
    });

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

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