1

我有一个表单,希望用户能够根据需要多次复制一组字段。对于一个组,它可以正确迭代,但是当我添加第二组时,“当前”变量会集体迭代,而不是每个组都是唯一的......我尝试将所有“当前”更改为“newFieldset.current”,但返回 NAN。 .. 有任何想法吗?

<script type="text/javascript">
$(document).ready(function() {
var current = 0;
    //Add New Fieldset with Button
    var newFieldset = {
        init: function(groupIndex) {
            current++;
            $newPerson= $("#Template"+groupIndex).clone(true);
            $newPerson.children("p").children("label").each(function(i) {
                var $currentElem= $(this);
                $currentElem.attr("for",$currentElem.attr("for")+current);
            });
            $newPerson.children("p").children("input").each(function(i) {
                var $currentElem= $(this);
                $currentElem.attr("name",$currentElem.attr("name")+current);
                $currentElem.attr("value",$currentElem.attr("value")+groupIndex+current);
                $currentElem.attr("id",$currentElem.attr("id")+current);
            });

            $newPerson.appendTo("#mainField"+groupIndex);
            $newPerson.removeClass("hideElement");
        },
        currentID: null,
        obj: null
    };
    $(".addButton").each(function() {
        $(this).click(function() {
            var groupIndex = $(this).attr("title");
            //newFieldset.obj = this;
            //var fieldIndex = $(this).attr("class");
            newFieldset.init(groupIndex);
        });
    });

    console.log('r');
});
</script>
<style>
.hideElement {display:none;}
</style>

<form name="demoForm" id="demoForm" method="post" action="#">
<div id="groupCtr1">
    <fieldset id="mainField1">
    <div id="Template1" class="hideElement">
    <p>
        <label for="firstname">Name</label> <em>*</em>
        <input id="firstname" name="firstname" size="25" /> <input id="lastname" name="lastname" size="25" />
    </p>
    <p>
        <label for="email">Email</label> <em>*</em><input id="email" name="email" size="25" />
    </p>
    </div>
    <div>
    <p>
        <label for="firstname1">Name</label> 
        <em>*</em> <input id="firstname1" name="firstname1" size="25" /> <input id="lastname1" name="lastname1" size="25" />
    </p>
    <p>
        <label for="email1">Email</label>  
        <em>*</em><input id="email1" name="email1" size="25" />
    </p>
    </div>
    </fieldset>
    <p>
    <input type="button" class="addButton" title="1" value="Add Another Person">
    </p>
</div>
<div id="groupCtr2">
    <fieldset id="mainField2">
    <div id="Template2" class="hideElement">
        <p>
            <label for="coname">Company Name</label> <em>*</em>
            <input id="coname" name="coname" size="25" />
        </p>
        <p>
            <label for="codesc">Description</label> <em>*</em><input id="codesc" name="codesc" size="25" />
        </p>
    </div>
    <div>
        <p>
            <label for="coname1">Company Name</label> 
            <em>*</em> <input id="coname1" name="coname1" size="25" />
        </p>
        <p>
            <label for="codesc1">Description</label>  
            <em>*</em><input id="codesc1" name="codesc1" size="25" />
        </p>
    </div>
    </fieldset>
    <p>
    <input type="button" class="addButton" title="2" value="Add Another Company">
    </p>
</div>
<input type="submit" value="Save">
</form>
4

3 回答 3

0

如果您不使当前变量成为全局变量,它应该可以工作。试试这个:

var newFieldset = { 
                  current: 0,
                  init: function() {
                      this.current++; 
                      //rest of init code
                  },
                  //all your other fieldset code here
};
/* all other code */

编辑:重新阅读问题后,我会对您想要实现的目标采取完全不同的方法。上面的代码仍然会为您表现出相同的行为。如果问题没有得到成功回答,我回家后会写一篇更大的文章。

于 2009-04-15T03:04:15.280 回答
0

我会做这样的事情:

...
var currents = {};
var newFieldset = {
    init: function(groupIndex) {
        var current = 0;
        if (currents[groupIndex]) {
            current = currents[groupIndex];
        }
        ++current;
        currents[groupIndex] = current;
...
于 2009-04-15T08:15:37.933 回答
0

使用 jQuery 数据方法将值附加到元素。单击时将其递增,然后将其作为第二个参数传递给 newFieldset.init 方法。瞧!

$(document).ready(function() {
    //Add New Fieldset with Button
    var newFieldset = {
        init: function(groupIndex,current) {
                $newPerson= $("#Template"+groupIndex).clone(true);
                $newPerson.children("p").children("label").each(function(i) {
                        var $currentElem= $(this);
                        $currentElem.attr("for",$currentElem.attr("for")+current);
                });
                $newPerson.children("p").children("input").each(function(i) {
                        var $currentElem= $(this);
                        $currentElem.attr("name",$currentElem.attr("name")+current);
                        $currentElem.attr("value",$currentElem.attr("value")+groupIndex+current);
                        $currentElem.attr("id",$currentElem.attr("id")+current);
                });

                $newPerson.appendTo("#mainField"+groupIndex);
                $newPerson.removeClass("hideElement");
        },
        currentID: null,
        obj: null
    };
    $(".addButton").click(function() {
        var groupIndex = $(this).attr("title");
        var current = $(this).data('current');
        $(this).data('current',++current);
        //newFieldset.obj = this;
        //var fieldIndex = $(this).attr("class");
        newFieldset.init(groupIndex,current);
    }).data('current',0);

    console.log('r');
});

先生,祝你快乐。

于 2009-04-15T15:39:52.797 回答