简体   繁体   English

从表单添加动态生成的字段

[英]add dynamically generated fields from form

Hi guys i want to dynamically add a group of input fields when a button is pressed. 嗨,大家好,我想在按下按钮时动态添加一组输入字段。 This works with 1 group, but not with more than one. 这适用于1个群组,但不能多个。 Here is my HTML: 这是我的HTML:

<form action="address.php" method="POST">
    <div id="list">
    <input type="text" name="address" placeholder="Address">
    <input type="text" name="suburb" placeholder="Suburb">
    <input type="text" name="state" placeholder="State">
    <input type="text" name="country" placeholder="Country">
    <button id="addMore">Add Address</button>
    </div>
</form>

I'm calling the addMore function with this javascript: 我正在使用此javascript调用addMore函数:

$(function(){
               $("#addMore").click(function(e){
                  e.preventDefault();
                   $("#list").append("<input type="text" name="address" placeholder="Address"><input type="text" name="suburb" placeholder="Suburb"><input type="text" name="state" placeholder="State"><input type="text" name="country" placeholder="Country"><button id="addMore2">Add Address</button></div>");
               });
            });

I've added a button at the end with an id of addMore2 because i wanna generate a similar set of input controls but with different names. 我在ID的末尾添加了一个按钮,其ID为addMore2,因为我想生成一组类似的输入控件,但名称不同。 I want to call that id with this function: 我想用这个功能叫那个ID:

$(function(){
               $("#addMore2").click(function(e){
                  e.preventDefault();
                   $("#list").append("<input type="text" name="address2" placeholder="Address"><input type="text" name="suburb2" placeholder="Suburb"><input type="text" name="state2" placeholder="State"><input type="text" name="country2" placeholder="Country"><button id="addMore3">Add Address</button></div>");
               });
            });

... and then another set of controls with the function addMore3. ...,然后是另一组带有addMore3功能的控件。 Same as above, but with the number 3. If i use each function alone, it works. 与上述相同,但编号为3。如果我单独使用每个功能,则可以使用。 But if i try to use all 3 together, it doesn't work. 但是,如果我尝试同时使用所有3种,则无法正常工作。 How can i dynamically reproduce a set of input controls with different names? 如何动态重现一组具有不同名称的输入控件?

you could do something like this 你可以做这样的事情

$(var count = 0;
           $("#addMore2").click(function(e){
              e.preventDefault();
               $("#list").append("<input type='text' name='address2'"+count+" placeholder="Address"><input type="text" name='suburb2'"+count+" placeholder="Suburb"><input type="text" name='state2'"+count+" placeholder="State"><input type="text" name='country2'"+count+" placeholder="Country"><button id='addMore3'"+count+">Add Address</button></div>");
           count++;
           });
        });

@rayzor @rayzor

You need to append your code before button 您需要在按钮之前附加代码

Please use below code: 请使用以下代码:

jQuery("#addMore").click(function(e){                  
    jQuery('<br><input type="text" name="address" placeholder="Address"><input type="text" name="suburb" placeholder="Suburb"><input type="text" name="state" placeholder="State"><input type="text" name="country" placeholder="Country">').insertAfter("input:last");
    return false;
});

And remove code for $("#addMore2").click event 并删除$("#addMore2").click事件的代码

There's no need to add Addmore 2,3,etc manually. 无需手动添加Addmore 2,3等。 the js will add it automatically, as much as your want. js会根据您的需要自动添加它。 Feel free to see this one: https://phppot.com/jquery/jquery-ajax-inline-crud-with-php/ 随意看一看: https : //phppot.com/jquery/jquery-ajax-inline-crud-with-php/

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

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