如何创建其他字段并从表单中删除它们?

时间:2013-08-08 03:00:02

标签: javascript jquery forms pug

我正在尝试允许用户添加其他电子邮件字段,如果他们想要添加其他电子邮件帐户,如果他们点击他们不想添加该字段,请点击删除。但是,我无法让删除按钮工作,我也是,试图以某种方式通过在每个类中包含一个变量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

3 个答案:

答案 0 :(得分:0)

你可以试试这个:

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

如果你想多次添加或删除,请确保使用正确的值设置emailCount,在添加时你没有再设置它,当你删除时你也没有改变我。

答案 1 :(得分:0)

我认为你可能需要在remove函数中将emailcount转换为类格式。类似的东西:

var classifiedEmailCount = "." + emailCount;
$(stringifiedEmailCount).remove();
i--;

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

  });

我不再需要多个字段了。