简体   繁体   English

preventDefault()停止提交jquery表单值

[英]preventDefault() stopping jquery form values being submitted

I have been trying to create an invitation only system for a wordpress site I'm working on (trying to avoid plugins where possible). 我一直在尝试为我正在工作的wordpress网站创建仅邀请系统(尝试尽可能避免使用插件)。

The site is a world of warcraft guild website and I have a page listing new applicants to the guild, with Accept and Reject options. 该网站是魔兽世界公会网站,我有一个页面列出该公会的新申请人,并带有接受和拒绝选项。 That is all working fine. 一切正常。

However, I'm trying to do the form submission via ajax and that is where I'm getting confused. 但是,我试图通过ajax提交表单,这让我感到困惑。 I have my form 我有我的表格

<?php
    global $current_user;
    // if the user can send invites
    if (current_user_can("invite_users")) {
    ?>
    <form action="applicantaccept" class="applicantacceptform" method="GET">
        <input type="hidden" name="email" value="<?php echo $row['Email'];?>"  />
        <input type="hidden" name="invitecode" value="<?php echo generateRandomString(); ?>" />
        <input style="background: none; border: none; color: white; text-decoration: underline; font-size: 1em; float: left;" type="submit" value="Send Invitation" />
    </form>
    <?php
    }
?>

And I have my jquery: 我有我的jQuery:

// ACCEPT
$('.applicantacceptform').submit(function (ev) {
    // INVITE FORM
    var form = $(this);

    var email = form.find('input[name="email"]').val();
    var invitecode = form.find('input[name="invitecode"]').val();

    $.post("applicantaccept", {
        email: "" + email + "",
        invitecode: "" + invitecode + ""
    })
        .done(function (data) {
        alert("Applicant Accepted" + data);
    })
        .fail(function (data) {
        alert("Failed To Accept Applicant. Please speak to administrator if this problem continues");
    });
ev.preventDefault();
});

When I submit with the ev.preventDefault() present, the alert shows success. 当我使用ev.preventDefault()提交时,警报显示成功。 however the values are parsed as empty strings through to the target page giving me blank sql records. 但是,这些值被解析为空字符串,直达目标页面,为我提供了空白的sql记录。 However, if I remove the preventdefault, I'm ofcourse following the link, but the values are now sent through correctly. 但是,如果删除了preventdefault,我当然会跟随该链接,但是现在值已正确发送。

What could be causing this? 是什么原因造成的?


Solved: Thanks to @Pointy for the fix, working code below (simply switched "GET" in the form to "POST" 解决:感谢@Pointy提供的修复,下面的工作代码(将表格中的“ GET”简单地切换为“ POST”

   <?php
global $current_user;
// if the user can send invites
if (current_user_can("invite_users")) {
    ?>
    <form action="applicantaccept" class="applicantacceptform" method="POST">
        <input type="hidden" name="email" value="<?php echo $row['Email'];?>"  />
        <input type="hidden" name="invitecode" value="<?php echo generateRandomString(); ?>" />
        <input style="background: none; border: none; color: white; text-decoration: underline; font-size: 1em; float: left;" type="submit" value="Send Invitation" />
    </form>
    <?php
}
?>

Have you tried return false; 您是否尝试过return false; instead of ev.preventDefault(); 而不是ev.preventDefault(); ?

Put the ev.preventDefault() as the first line in that function. ev.preventDefault()作为该函数的第一行。 My guess is it's trying to follow the link before firing the form submit. 我的猜测是,在触发表单提交之前,它试图遵循链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM