使用jquery进行多个javascript变量迭代

时间:2009-04-15 02:57:55

标签: javascript jquery forms iteration

我有一个表单,并希望用户能够根据需要多次复制一组字段。有一个组,它正确迭代,但当我添加第二组时,“当前”变量集体迭代而不是每个组唯一...我尝试将所有“当前”更改为“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>

3 个答案:

答案 0 :(得分:0)

如果你不将当前变量设为全局,那么应该有效。试试这个:

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

编辑:重新阅读问题后,我会采用完全不同的方法来实现您的目标。上面的代码仍会显示相同的行为。如果问题没有成功回答,我回家后会做更大的写作。

答案 1 :(得分:0)

我会做那样的事情:

...
var currents = {};
var newFieldset = {
    init: function(groupIndex) {
        var current = 0;
        if (currents[groupIndex]) {
            current = currents[groupIndex];
        }
        ++current;
        currents[groupIndex] = current;
...

答案 2 :(得分: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');
});

先生,快乐的问候。