如何获得动态添加文本框的价值?

时间:2023-01-28 18:08:10

I have a code in jquery to add and remove the textboxes dynamically on button click. I want to post the values of dynamically created text on same page.

我在jquery中有一个代码,可以在按钮点击时动态添加和删除文本框。我想在同一页面上发布动态创建的文本的值。

the code is here :

代码在这里:

<div id="itemRows">

Item quantity: <input type="text" name="add_qty" size="4" /> 
Item name: <input  type="text" name="add_name" /> 
Item Price: <input type="text" name="add_price" /> 
Total: <input type="text" name="total" /> 
<input onclick="addRow(this.form);" type="button" value="Add row" /> 
</div>

<script type="text/javascript">
var rowNum = 0;
function addRow(frm) {
rowNum ++;
var row = '<p id="rowNum'+rowNum+'">Item quantity: <input type="text" name="qty[]" size="4" value="'+frm.add_qty.value+'"> Item name: <input type="text" name="name[]" value="'+frm.add_name.value+'"> Item Price: <input type="text" name="price[]" value="'+frm.add_price.value+'">Total: <input type="text" name="total[]" value="'+frm.total.value+'"> <input type="button" value="Remove" onclick="removeRow('+rowNum+');"></p>';
jQuery('#itemRows').append(row);
frm.add_qty.value = '';
frm.add_name.value = '';
}

function removeRow(rnum) {
jQuery('#rowNum'+rnum).remove();
}
</script>

1 个解决方案

#1


0  

Well you already have correct names for each field, like qty[], name[] etc. so when these values are posted to some page, each of them will appear in array. For example, you have 3 boxes with values of name and qty filled and they are sent. This is how they will appear:

那么你已经有了每个字段的正确名称,比如qty [],name []等等。所以当这些值发布到某个页面时,每个字段都会出现在数组中。例如,您有3个框,其中包含名称和数量的值,并且它们将被发送。这是它们的出现方式:

[name] => Array
    (
        [0] => name1
        [1] => name2
        [2] => name3
        [3] => name4
    )
[qty] => Array
    (
        [0] => qty1
        [1] => qty2
        [2] => qty3
        [3] => qty4
    )

So, name[0] will have qty[0] too as they are in same index order. Now lets say you want to save them in database, this is how you can do it:

因此,name [0]也将具有qty [0],因为它们处于相同的索引顺序中。现在假设您要将它们保存在数据库中,这是您可以这样做的:

$data=array();
foreach($_POST['name'] as $key=>$val){  //$key is index, and $val is value posted
    $data[] = array(
         'name' =>  $_POST['name'][$key] //it will add name of that index to this array... 
        ,'qty'  =>  $_POST['qty'][$key]  //it will add qty of that index to this array...
        //and rest of yoru posted data too, like price etc etc...
    );
}

Now you have an array of information where you have all the data in organised form. If you are not sure what it is doing just print_r($data) and you will see what it has made for you.

现在,您拥有一系列信息,其中包含有组织形式的所有数据。如果你不确定它是做什么只是print_r($ data),你会看到它为你做了什么。

echo "<pre>";
    print_r($data);
echo "<pre>";

I am sure, it is easy now for you to play around with this $data array.

我相信,现在很容易玩这个$ data数组。

#1


0  

Well you already have correct names for each field, like qty[], name[] etc. so when these values are posted to some page, each of them will appear in array. For example, you have 3 boxes with values of name and qty filled and they are sent. This is how they will appear:

那么你已经有了每个字段的正确名称,比如qty [],name []等等。所以当这些值发布到某个页面时,每个字段都会出现在数组中。例如,您有3个框,其中包含名称和数量的值,并且它们将被发送。这是它们的出现方式:

[name] => Array
    (
        [0] => name1
        [1] => name2
        [2] => name3
        [3] => name4
    )
[qty] => Array
    (
        [0] => qty1
        [1] => qty2
        [2] => qty3
        [3] => qty4
    )

So, name[0] will have qty[0] too as they are in same index order. Now lets say you want to save them in database, this is how you can do it:

因此,name [0]也将具有qty [0],因为它们处于相同的索引顺序中。现在假设您要将它们保存在数据库中,这是您可以这样做的:

$data=array();
foreach($_POST['name'] as $key=>$val){  //$key is index, and $val is value posted
    $data[] = array(
         'name' =>  $_POST['name'][$key] //it will add name of that index to this array... 
        ,'qty'  =>  $_POST['qty'][$key]  //it will add qty of that index to this array...
        //and rest of yoru posted data too, like price etc etc...
    );
}

Now you have an array of information where you have all the data in organised form. If you are not sure what it is doing just print_r($data) and you will see what it has made for you.

现在,您拥有一系列信息,其中包含有组织形式的所有数据。如果你不确定它是做什么只是print_r($ data),你会看到它为你做了什么。

echo "<pre>";
    print_r($data);
echo "<pre>";

I am sure, it is easy now for you to play around with this $data array.

我相信,现在很容易玩这个$ data数组。