jQuery 克隆明文字段

发布于 2024-11-27 04:22:57 字数 171 浏览 0 评论 0原文

当您单击“添加其他朋友”时,我试图让朋友字段(姓名+电子邮件)重复。 每次我尝试克隆字段时,它都会在字段中包含我不想要的文本! 有人有什么想法吗?

jsFiddle

提前致谢:)

I am trying to get the friend fields (name + email) to repeat when you click "add another friend".
Every time I try to clone the fields it includes the text inside the fields which I dont want!
Has anybody got any ideas?

jsFiddle

Thanks in advance :)

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

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

发布评论

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

评论(2

心清如水 2024-12-04 04:22:57

将点击事件处理程序绑定到“添加另一个朋友”

元素。在该事件处理程序中,您希望将另一行表单元素附加到

元素。

HTML:

<p id="friendlist">Friend 1 Name:
    <input type="text" name="friend_name[]" class="friendid">
    Friend 1 Email:
    <input type="text" name="friend_email[]" class="friendid"/></p>

<div id="addanother">add another friend</div>

<input type="submit" name="submit" />

您的 jQuery:

$(function() {
    var nextIndex = 2;

    $("#addanother").click(function() {
        var html = '<br />Friend ' + nextIndex + 'Name:';
        html += '<input type="text" name="friend_name[]" class="friendid" />'
        html += 'Friend ' + nextIndex + 'Email:';
        html += '<input type="text" name="friend_email[]" class="friendid" />';
        $("#friendlist").append($(html));
        nextIndex++;
    });
});

jsFiddle

Bind a click event handler to the "add another friend" <div> element. In that event handler, you want to append another row of form elements to the <p> element.

HTML:

<p id="friendlist">Friend 1 Name:
    <input type="text" name="friend_name[]" class="friendid">
    Friend 1 Email:
    <input type="text" name="friend_email[]" class="friendid"/></p>

<div id="addanother">add another friend</div>

<input type="submit" name="submit" />

Your jQuery:

$(function() {
    var nextIndex = 2;

    $("#addanother").click(function() {
        var html = '<br />Friend ' + nextIndex + 'Name:';
        html += '<input type="text" name="friend_name[]" class="friendid" />'
        html += 'Friend ' + nextIndex + 'Email:';
        html += '<input type="text" name="friend_email[]" class="friendid" />';
        $("#friendlist").append($(html));
        nextIndex++;
    });
});

jsFiddle

君勿笑 2024-12-04 04:22:57

从字段中获取值后尝试设置字段 value=''

Try setting the fields value='' after you take the values from them

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