简体   繁体   English

Bootstrap表单动态添加带有字段的删除表单

[英]Bootstrap form dynamically add remove form with fields

I have a Bootstrap horizontal form (with many fields) and would like functionality to add/remove one or many "sub-forms (with many fields) to the parent form. 我有一个Bootstrap水平表单(具有多个字段),并且希望将功能添加或删除一个或多个“子表单(具有多个字段)到父表单”。

Also I'm not sure which path to go down, ie code with Bootstrap components, or purely with jQuery and CSS (within Bootstrap), or JavaScript only (within Bootstrap). 另外,我不确定要走哪条路径,即使用Bootstrap组件编码,或者仅使用jQuery和CSS(在Bootstrap中)编码,或者仅使用JavaScript(在Bootstrap中)编码。

This may be a simple question, and I've been searching for a bread and butter solution, but with no luck. 这可能是一个简单的问题,我一直在寻找一种解决方案,但是没有运气。 Perhaps it's very difficult to do. 也许这很难做到。

Found this, but no help Similar question on SO 发现这个,但没有帮助, 对如此的相似问题

And this, but it's done in JavaScript Another question on SO 这样,但是它是用JavaScript完成的关于SO的另一个问题

Here is an example of what I'd like to do, I have a parent form which contains personal details fields, and would like when a user clicks a button (+) an address form is dynamically added, and if button (-) is clicked, the address form is removed. 这是我要执行的操作的示例,我有一个包含个人详细信息字段的父表单,并且希望当用户单击按钮(+)时动态添加地址表单,并且如果按钮(-)为单击后,地址表格将被删除。

Person

Firstname: 名字:

Surname: 姓:

Email: 电子邮件:

Button (+/-) 按钮(+/-)


Address 1 地址1

Street: 街:

Suburb: 市郊:

Postcode: 邮编:


Address 2 地址2

Street: 街:

Suburb: 市郊:

Postcode: 邮编:

I've done something like you are talking about with Kendo. 我做的事情就像您在与剑道谈论。 Basically, you need a template and add/remove item capability. 基本上,您需要一个模板并添加/删除项目功能。 Using any templating capability, you can define a template, and dynamically generate the template based on a JSON object, and add it to the DOM tree, or remove an existing item from the DOM tree with jQuery. 使用任何模板功能,您都可以定义模板,并基于JSON对象动态生成模板,然后将其添加到DOM树中,或者使用jQuery从DOM树中删除现有项。

A template is a great option and probably the way you should go. 模板是一个很好的选择,可能是您应该采用的方式。 However, if you want to avoid adding a templating engine, you can do it with jQuery, or even just vanilla JS. 但是,如果要避免添加模板引擎,则可以使用jQuery,甚至可以使用普通JS。

Here is simple JQuery example: 这是简单的JQuery示例:

  $(function () {
    function createGroup(name) {
        var label = document.createElement("label");
        $label = $(label);
        $label.attr("for", name)
            .text(name);

        var input = document.createElement("input");
        $input = $(input);
        $input.addClass("form-control")
            .attr("type", "text")
            .attr("name", name);

        var group = document.createElement("div");
        $group = $(group);
        $group.addClass("form-group")
            .append(label, input);

        return group;
    }

    $("#plusButton").click(
        function () {
            var newHorizontalForm = document.createElement("div");
            $(newHorizontalForm).append(createGroup("New Input"),
                    createGroup("New Input"),
                    createGroup("New Input"))
                .addClass("form-horizontal");
                $("#form").append(newHorizontalForm);
    });


     $("#minusButton").click(
            this.parent.remove();
        });

});

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

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