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

发布于 2024-07-18 04:01:41 字数 3864 浏览 6 评论 0原文

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

i have a form and would like to give users the ability to duplicate a group of fields as many times as necessary. With one group it iterates correctly but when I add a second group the "current" variable iterates collectively instead of being unique to each group... i tried changing all of the "current" to "newFieldset.current" but that returns NAN... any ideas?

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落花随流水 2024-07-25 04:01:42

我会做类似的事情:

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

I would do something like that:

...
var currents = {};
var newFieldset = {
    init: function(groupIndex) {
        var current = 0;
        if (currents[groupIndex]) {
            current = currents[groupIndex];
        }
        ++current;
        currents[groupIndex] = current;
...
还给你自由 2024-07-25 04:01:41

使用 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');
});

先生,祝您使用 jquery 愉快。

Attach the value to the element with the jQuery data method. Increment it on click, and then pass it to the newFieldset.init method as the second param. Voila!

$(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');
});

Happy jquery-ing to you sir.

自我难过 2024-07-25 04:01:41

如果你不将当前变量设置为全局变量,它will应该可以工作。 试试这个:

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

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

if you dont make the current variable global it will should work. try this:

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

EDIT: After re-reading the question, I would take a completely different approach to what you're trying to achieve. The above code will still exhibit the same behavior for you. If the question hasn't been successfully answered I'll do a bigger writeup when i get home.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文