簡體   English   中英

創建無限擴展的形式?

[英]create an infinitely extensible form?

我有一個表格,需要能夠將表​​格中的特定行相乘,然后復制整個表格。 在我正在研究的概念示例中,有一個“名稱”字段和一個“ URL”字段。 我需要1個名稱字段和3個URL字段,但是我需要將此整個分組重復最多3次,然后通過POST提交,但數字配對可以不同。
例如,第一個組可以具有1個名稱,3個URL,第二個組可以具有1個URL,而第三個組可以具有3個URL。 我可以乘以URL字段或整個表單的數量,但是如果我有2個URL字段,那么表單的所有倍數都有2個URL字段,因此我無法在任何表單中進行更改。

與僅在此處發布代碼相比,查看JSFiddle要容易得多,因為您可以看到我的代碼及其行為方式。 http://jsfiddle.net/wingdom/yCTpf/3/

感謝您的幫助!

HTML:

<form id="myForm">
<div id="O1" class="clonedInput5">
    <fieldset>
        <input type="hidden" name="Ocount" id="Ocount" value="1" />
        <legend>Outbound App - Required</legend>
        <div>
            <label>Name:</label>
            <input type="text" name="oname">
        </div>
        <div id="ourl1" style="margin-bottom:4px;" class="clonedInput1">URL:
            <input type="text" name="ourl1" id="ourl1" />
        </div>
        <div>
            <input type="button" id="ourlAdd" value="Add URL" />
            <input type="button" id="ourlDel" value="Remove URL" />
        </div>
        <input type="hidden" name="urlo" id="urlo" value="1" />
    </fieldset>
</div>
<input type="button" id="OAdd" value="Add Outbound" />
<input type="button" id="ODel" value="Rm Outbound" />

使用Javascript:

$('#ourlAdd').click(function () {
var num = $('.clonedInput1').length; // how many "duplicatable" input fields we currently have
var newNum = new Number(num + 1); // the numeric ID of the new input field being added

// create the new element via clone(), and manipulate it's ID using newNum value
var newElem = $('#ourl' + num).clone().attr('id', 'ourl' + newNum);

// manipulate the name/id values of the input inside the new element
newElem.children(':first').attr('id', 'ourl' + newNum).attr('name', 'ourl' + newNum);

// insert the new element after the last "duplicatable" input field
$('#ourl' + num).after(newElem);

document.getElementById("urlo").value = newNum;


// enable the "remove" button
$('#ourlDel').attr('disabled', '');

// business rule: you can only add 5 names
if (newNum == 3) $('#ourlAdd').attr('disabled', 'disabled');
});

$('#ourlDel').click(function () {
var num = $('.clonedInput1').length; // how many "duplicatable" input fields we currently have
$('#ourl' + num).remove(); // remove the last element

document.getElementById("urlo").value = num - 1;

// enable the "add" button
$('#ourlAdd').attr('disabled', '');

// if only one element remains, disable the "remove" button
if (num - 1 == 1) $('#ourlDel').attr('disabled', 'disabled');
});

$('#ourlDel').attr('disabled', 'disabled');

$('#OAdd').click(function () {
var num = $('.clonedInput5').length; // how many "duplicatable" input fields we currently have
var newNum = new Number(num + 1); // the numeric ID of the new input field being added

// create the new element via clone(), and manipulate it's ID using newNum value
var newElem = $('#O' + num).clone().attr('id', 'O' + newNum);



// insert the new element after the last "duplicatable" input field
$('#O' + num).after(newElem);

document.getElementById("Ocount").value = newNum;


// enable the "remove" button
$('#ODel').attr('disabled', '');

// business rule: you can only add 5 names
if (newNum == 3) $('#OAdd').attr('disabled', 'disabled');
});

$('#ODel').click(function () {
var num = $('.clonedInput5').length; // how many "duplicatable" input fields we currently have
$('#O' + num).remove(); // remove the last element

document.getElementById("Ocount").value = num - 1;

// enable the "add" button
$('#OAdd').attr('disabled', '');

// if only one element remains, disable the "remove" button
if (num - 1 == 1) $('#ODel').attr('disabled', 'disabled');
});

$('#ODel').attr('disabled', 'disabled');

一種方法是首先存儲原始表單的副本,然后使用該副本創建其他表單。

var $originalForm = $("#originalForm").clone();

現在,任何時候您需要創建一個新表單時,只需執行以下操作: $originalForm.clone()然后遍歷表單中固定ID的元素(假設您不會不使用id)。

雖然,我個人將以完全不同的方式進行此操作。

var formTemplate = "form template here, or get it from a <script type=\"text/template\"></text>";

function ExtendableForm() {
    this.$form = $(formTemplate).appendTo("body");
    this.bindEvents();
    return this.$form;
}
$.extend( ExtendableForm.prototype, {
    addAnotherURL: function(){
        this.$form.find(".wrap-url").append($(formTemplate).find(".wrap-url").children());
    },
    addAnotherName: function(){
        this.$form.find(".wrap-name").append($(formTemplate).find(".wrap-url").children());
    },
    bindEvents: function(){
        this.$form
            .on("click", ".add-url", $.proxy(addAnotherURL,this))
            .on("click", ".add-name", $.proxy(addAnotherName,this));
    }
});



$("#addAnotherForm").click(function(){
    $("#form-container").append(new ExtendableForm());
}).click();

它可能可以做得更干一點,但這是基本思想。 沒有人知道或需要關心任何其他形式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM