【问题标题】:Email being sent out multiple times电子邮件被多次发送
【发布时间】:2017-10-24 19:40:32
【问题描述】:

我编写了一个 javascript 代码来处理来自联系表单的输入。我使用 Ajax 将输入信息发送到后端以发送电子邮件。

但是,我的代码似乎以某种方式发送了多封电子邮件,而不是一封。 “电子邮件已发送”消息显示了几次。

谁能告诉我出了什么问题?

JAVASCRIPT 代码:

$(document).on('click', '.individual_contact', function(e) {
    e.preventDefault();
    var user_name = $('span#user-name').text();
    var recipient_name = $(this).attr('data-ind');
    console.log('recipient_name='+recipient_name);

    $("div#content").hide('fast');
    $("#section-form").show('fast');
    $("#section-form #recipient").attr('data-contact', recipient_name);
    $("#section-form #recipient").attr('placeholder', 'TO:  '+recipient_name.toUpperCase());
    $("#section-form #name").val('FROM:  '+user_name.toUpperCase());

    $('.submit_icontact').click(function(e) {
        var subject = $('input#subject').val();
        var message = $('textarea#message').val();

        e.preventDefault();
        var form = new FormData();
        form.append('user_email', ajaxobject.user_id);
        form.append('user_name', user_name);
        form.append('recipient_name', recipient_name);
        form.append('subject', subject);
        form.append('message', message);
        form.append('action', 'contact_individual');
        console.log(form);

        $.ajax({
            type: 'POST',
            url: ajaxobject.ajaxurl,
            enctype: 'multipart/form-data',
            cache: false,
            contentType: false,
            processData: false,
            data: form,
            dataType: 'json',
            success: function(response) {
                console.log('Email is sent');
            },
            error:function(err){
                console.log('err,error')
            }
        });
    });
})

【问题讨论】:

  • 每次点击 .individual_contact 都会给 .submit_icontact 添加另一个事件处理程序,所以点击 .individual_contact 一次意味着点击 .submit_icontact 发送 1 封电子邮件,点击 .individual_contact 两次意味着 .submit_icontact 发送 2 封电子邮件等。您应该只将事件处理程序分配给 .submit_icontact 一次。
  • 感谢您的解释。你是对的。 @詹姆斯

标签: javascript php html wordpress


【解决方案1】:

每次单击.individual_contact 时,都会向.submit_icontact 添加另一个事件侦听器。

将您的 .submit_icontact 点击事件移出您的 .individual_contact 点击处理程序

【讨论】:

  • 非常感谢您的回答。移动 .submit_icontact 确实解决了这个问题。
  • 这应该是公认的答案,因为它解释了多次提交的根本原因。谢谢。
【解决方案2】:

更改:

$('.submit_icontact').click(function(e) {

收件人:

$('.submit_icontact').unbind().click(function(e) {

【讨论】:

  • 这正是我想要的!非常感谢!
  • ApplePie,您是否有机会将我的答案标记为解决方案/正确答案?谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-04-14
  • 1970-01-01
  • 2018-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 1970-01-01
相关资源
最近更新 更多