Add / remove input field dynamically with jQuery

前端 未结 12 1904
闹比i
闹比i 2020-12-02 06:14

I would like to use jquery to build a dynamic add/ remove form. IT should look like:

Name Type Required?

The example input :

  • Name Type Require
12条回答
  •  感动是毒
    2020-12-02 06:37

    Jquery Code

    $(document).ready(function() {
        var max_fields      = 10; //maximum input boxes allowed
        var wrapper         = $(".input_fields_wrap"); //Fields wrapper
        var add_button      = $(".add_field_button"); //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(''); //add input box
            }
        });
    
        $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
            e.preventDefault(); $(this).parent('div').remove(); x--;
        })
    });
    

    HTML CODE

提交回复
热议问题