Add / remove input field dynamically with jQuery

前端 未结 12 1744
闹比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:45

    You need to create the element.

    input = jQuery('<input name="myname">');
    

    and then append it to the form.

    jQuery('#formID').append(input);
    

    to remove an input you use the remove functionality.

    jQuery('#inputid').remove();
    

    This is the basic idea, you may have feildsets that you append it too instead, or maybe append it after a specific element, but this is how to build anything dynamically really.

    0 讨论(0)
  • 2020-12-02 06:45

    Another solution could be:

    <script>
    $(document)
            .ready(
                    function() {
                        var wrapper = $(".myFields");
                        $(add_button)
                                .click(
                                        function(e) {
                                            e.preventDefault();
                                            $(wrapper)
                                                    .append(
                                                            '.....'); //add fields here
                                        });
                        $(wrapper).on("click", ".delFld", function(e) {
                            e.preventDefault();
                            $(this).parent('div').remove();
                        })
                    }); 
    </script>
    

    Source: Here

    0 讨论(0)
  • 2020-12-02 06:47

    In your click function, you can write:

    function addMoreRows(frm) {
       rowCount ++;
       var recRow = '<p id="rowCount'+rowCount+'"><tr><td><input name="" type="text" size="17%"  maxlength="120" /></td><td><input name="" type="text"  maxlength="120" style="margin: 4px 5px 0 5px;"/></td><td><input name="" type="text" maxlength="120" style="margin: 4px 10px 0 0px;"/></td></tr> <a href="javascript:void(0);" onclick="removeRow('+rowCount+');">Delete</a></p>';
       jQuery('#addedRows').append(recRow);
    }
    

    Or follow this link: http://www.discussdesk.com/add-remove-more-rows-dynamically-using-jquery.htm

    0 讨论(0)
  • 2020-12-02 06:52

    You can do something like this below:

    //when the Add Field button is clicked
    $("#add").click(function (e) {
    //Append a new row of code to the "#items" div
      $("#items").append('<div><input type="text" name="input[]"><button  class="delete">Delete</button></div>');
    });
    

    For detailed tutorial http://voidtricks.com/jquery-add-remove-input-fields/

    0 讨论(0)
  • 2020-12-02 06:53

    You can try this:

    <input type="hidden" name="image" id="input-image{{ image_row }}" />
    
    inputt= '<input type="hidden" name="product_image' value="somevalue">
    
    $("#input-image"+row).remove().append(inputt);
    
    0 讨论(0)
  • 2020-12-02 06:57

    I took the liberty of putting together a jsFiddle illustrating the functionality of building a custom form using jQuery. Here it is...

    EDIT: Updated the jsFiddle to include remove buttons for each field.

    EDIT: As per the request in the last comment, code from the jsFiddle is below.

    EDIT: As per Abhishek's comment, I have updated the jsFiddle (and code below) to cater for scenarios where duplicate field IDs might arise.

    HTML:

    <fieldset id="buildyourform">
        <legend>Build your own form!</legend>
    </fieldset>
    <input type="button" value="Preview form" class="add" id="preview" />
    <input type="button" value="Add a field" class="add" id="add" />
    

    JavaScript:

    $(document).ready(function() {
        $("#add").click(function() {
            var lastField = $("#buildyourform div:last");
            var intId = (lastField && lastField.length && lastField.data("idx") + 1) || 1;
            var fieldWrapper = $("<div class=\"fieldwrapper\" id=\"field" + intId + "\"/>");
            fieldWrapper.data("idx", intId);
            var fName = $("<input type=\"text\" class=\"fieldname\" />");
            var fType = $("<select class=\"fieldtype\"><option value=\"checkbox\">Checked</option><option value=\"textbox\">Text</option><option value=\"textarea\">Paragraph</option></select>");
            var removeButton = $("<input type=\"button\" class=\"remove\" value=\"-\" />");
            removeButton.click(function() {
                $(this).parent().remove();
            });
            fieldWrapper.append(fName);
            fieldWrapper.append(fType);
            fieldWrapper.append(removeButton);
            $("#buildyourform").append(fieldWrapper);
        });
        $("#preview").click(function() {
            $("#yourform").remove();
            var fieldSet = $("<fieldset id=\"yourform\"><legend>Your Form</legend></fieldset>");
            $("#buildyourform div").each(function() {
                var id = "input" + $(this).attr("id").replace("field","");
                var label = $("<label for=\"" + id + "\">" + $(this).find("input.fieldname").first().val() + "</label>");
                var input;
                switch ($(this).find("select.fieldtype").first().val()) {
                    case "checkbox":
                        input = $("<input type=\"checkbox\" id=\"" + id + "\" name=\"" + id + "\" />");
                        break;
                    case "textbox":
                        input = $("<input type=\"text\" id=\"" + id + "\" name=\"" + id + "\" />");
                        break;
                    case "textarea":
                        input = $("<textarea id=\"" + id + "\" name=\"" + id + "\" ></textarea>");
                        break;    
                }
                fieldSet.append(label);
                fieldSet.append(input);
            });
            $("body").append(fieldSet);
        });
    });
    

    CSS:

    body
    {
        font-family:Gill Sans MT;
        padding:10px;
    }
    fieldset
    {
        border: solid 1px #000;
        padding:10px;
        display:block;
        clear:both;
        margin:5px 0px;
    }
    legend
    {
        padding:0px 10px;
        background:black;
        color:#FFF;
    }
    input.add
    {
        float:right;
    }
    input.fieldname
    {
        float:left;
        clear:left;
        display:block;
        margin:5px;
    }
    select.fieldtype
    {
        float:left;
        display:block;
        margin:5px;
    }
    input.remove
    {
        float:left;
        display:block;
        margin:5px;
    }
    #yourform label
    {
        float:left;
        clear:left;
        display:block;
        margin:5px;
    }
    #yourform input, #yourform textarea
    {
        float:left;
        display:block;
        margin:5px;
    }
    
    0 讨论(0)
提交回复
热议问题