0

我正在尝试允许用户添加其他电子邮件字段,如果他们想添加其他电子邮件帐户,如果他们不想添加该字段,请点击删除。但是,我无法让删除按钮起作用,而且我也试图通过在每个类中包含一个变量 emailCount 来以某种方式区分每个类,但由于某种原因这也不起作用......

这是 jquery 脚本:(它在文档就绪函数中)

  var i = 0;
 $(function(){       
        i =+ 1;
        var emailCount = "Email" + i.toString();
        console.log(i);
        console.log(emailCount);
        $('.addEmail').click(function() {


            $('#addInfo').append('<div class="' + emailCount '"></div><form class="newEmail", method="post", action="newEmailPost"> <label>' + emailCount + '</label>' + '<input name="' + emailCount + '", type="email", value=""/><br><input type="submit", class="addNewEmail", value="Save Email"></input><button class="removeEmailField">Remove</button></form><br>');
        });

         $('.removeEmailField').click(function() {
            $(emailCount).remove();

        });
    });

这是玉文件:(它可以正常工作,但可能有助于视觉目的)

extends layout
block content   
    div.centerContent
        div
            form.validateForm(method="POST", action="/editUserProfile")
                    legend Edit Profile
                    input(type="text", name="firstName", maxlength="20", placeholder=ufirstName, value=ufirstName)
                    br
                    input(type="text", name="lastName", maxlength="20", placeholder=ulastName, value=ulastName)
                    br
                    input(type="email", name="email", maxlength="20", placeholder=uemail, value=uemail)
                    br
                    - if(uemailList.length > 0)
                        for userC in uemailListCount
                            for userE in uemailList
                                input(type="email", name=userC, maxlength="20", placeholder=userE, value=userE)
                                br
                    input(type="number", name="phone", maxlength="20", placeholder=uphone, value=uphone)
                    br
                    input(type="date", name="birthday", value=ubirthday)
                    br
                    input.btn.btn-primary(type="submit", name="Update", value="Save")
                    a(href="/userProfile")
                        button.btn(type="button") Cancel
                    hr
        div#addInfo
            label Add another email address:  
                button.addEmail Add Email
            br
            label Add another phone number:  
                button.addPhone Add Phone Number
4

3 回答 3

0

好的,我只是将其更改为如下所示:

$('.addEmail').click(function() {
            $('#addInfo').replaceWith('<div class="newEmailAdd"></div><form class="newEmail", method="post", action="/newEmailPost"> <label> Add Another Email </label>' + '<input name="emailNew", type="email", value=""/><br><input type="submit", class="addNewEmail", value="Save Email"></input><button class="removeEmailField">Remove</button></form><br>');
        });



 $('.removeEmailField').click(function() {
            alert('You clicked remove');
            $('.newEmailAdd').remove();

  });

我不再需要多个字段来工作了。

于 2013-08-08T04:44:18.887 回答
0

你可以试试这个:

$("."+emailCount).remove();
i--;

如果要多次添加或删除,请确保将 emailCount 设置为正确的值,添加时不要再次设置它,删除时也不要更改 i。

于 2013-08-08T03:05:32.553 回答
0

我认为您可能需要在删除函数中将 emailcount 转换为类格式。就像是:

var classifiedEmailCount = "." + emailCount;
$(stringifiedEmailCount).remove();
i--;
于 2013-08-08T03:06:42.947 回答