简体   繁体   中英

Dynamically Add Form Fields and Update Field Value

I have a form that you can add fields: http://jsfiddle.net/ytrkqr6a/2/

    $(document).ready(function() {
    var max_fields      = 32; //maximum input boxes allowed
    var wrapper         = $(".cameras"); //Fields wrapper
    var add_button      = $(".add-camera"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div class="form-group"><input type="text" name="camera '+ x +'" value="Camera '+ x +'" placeholder="Camera '+ x +'" class="form-control cameras" readonly /> <a href="#" class="remove-camera">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove-camera", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
<div class="cameras">
            <div class="camera-field"><button class="add-camera btn btn-primary">+ Add Camera</button></div>
            <div style="clear:both;"></div>

            <div class="form-group"><input type="text" name="camera" value="" placeholder="Camera 1" class="form-control cameras" readonly /></div>

            </div>

Everything works fine adding fields, but where I'm hung up is when you remove one of the fields then numerical order gets off.. how do I make it update the remaining fields to stay in proper numerical order each time you add or remove a field:

Camera 1
Camera 2
Camera 3
etc...

Thanks ahead of time!

I just added a jQuery "each" loop to re placerholder, name, and value each input field every time a remove link is clicked. When using each the value passed to the function is the index of item in the loop's current iteration, I used the variable elm. Since each input field is classed with camerasCounter, you can use $(this) to easily call the input element in the loop. Each is a very useful part of jQuery, if you ask me.

http://jsfiddle.net/v76zn30o/2/

The HTML

<div class="cameras">
            <div class="camera-field"><button class="add-camera btn btn-primary">+ Add Camera</button></div>
            <div style="clear:both;"></div>

            <div class="form-group"><input type="text" name="camera" value="" placeholder="Camera 1" class="form-control camerasCounter" readonly /></div>

            </div>

and The jQuery

$(document).ready(function() {
var max_fields      = 32; //maximum input boxes allowed
var wrapper         = $(".cameras"); //Fields wrapper
var add_button      = $(".add-camera"); //Add button ID

var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div class="form-group"><input type="text" name="camera '+ x +'" value="Camera '+ x +'" placeholder="Camera '+ x +'" class="form-control camerasCounter" readonly /> <a href="#" class="remove-camera">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove-camera", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove();

        $(".camerasCounter").each(function(elm){
            x = elm + 1
            $(this).attr({
                "placeholder": "Camera " + x,
                "name": "Camera " + x, 
                "value": "Camera " + x
            });             
        });



    });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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