简体   繁体   English

单击“提交”按钮时,Javascript函数会触发两次

[英]Javascript function fires twice on clicking 'Submit' button

I'm trying to insert records into DB using AJAX. 我正在尝试使用AJAX将记录插入数据库。 I'm not sure why, but it seems the javascript function referenced in the onclick tag of the submit button gets fired twice, and hence I get two records in my DB per click. 我不确定为什么,但似乎提交按钮的onclick标签中引用的javascript函数被触发两次,因此我在每次点击的数据库中得到两条记录。

Placing alerts in the JS, I have managed to figure out that the problem is in the JS function getting called twice, and not the PHP script making two inserts. 在JS中放置警报,我已经设法找出问题是在JS函数中调用两次,而不是PHP脚本进行两​​次插入。 So, I'm not posting the PHP script, unless asked. 所以,除非被问到,否则我不会发布PHP脚本。

Here's the HTML for the form: 这是表单的HTML:

<form id="notify" method="post" action="add_notify.php">            
            Name: <input type="text" class="formname" name="name" value="" size="20"/>
            Email: <input type="text" class="formname" name="email" value="" size="20"/>
            <input type="submit" class="sendform" name="submit" onclick="processInfo()" value="Go!"/>
</form>

Javascript: 使用Javascript:

$("document").ready(function() {
    $("#notify").submit(function() {
        processInfo();
        return false;
    });
});

function processInfo() 
{
    var errors = false;
    // Validate name
    var name = $("#notify [name='name']").val();
    if (!name) {
        errors = true;
        document.getElementById('name_error').innerHTML = 'You must enter a name.';
    }

    var email = $("#notify [name='email']").val();
    if (!email) 
    {
        errors = true;
        document.getElementById('email_error').innerHTML = 'You must enter an email.';
    }
    else
    {
        var validEmail = true;      

        validEmail = validateEmail(email);

        if (!validEmail)
        {
            errors = true;
            document.getElementById('email_error').innerHTML = 'You must enter a valid email address.';         
        }
    }

    if (!errors) 
    {
        $("#notify").ajaxSubmit({success:showResult});
        return false;
    }
}

You are calling processInfo twice once in submit handler and once in click handler. 您在提交处理程序中调用processInfo两次,在单击处理程序中调用一次。 This might be the reason. 这可能是原因。

Here onclick="processInfo()" and inside $("#notify").submit(function() { processInfo(); return false; }); 这里onclick="processInfo()"$("#notify").submit(function() { processInfo(); return false; });

processInfo() is called twice, both here, when the form submits: 当表单提交时,processInfo()在这里被调用两次:

$("#notify").submit(function() {
        processInfo();
        return false;
    });

and here, when you click the submit button: 在这里,当您单击提交按钮时:

<input type="submit" class="sendform" name="submit" onclick="processInfo()" value="Go!"/>

You should remove one of them. 你应该删除其中一个。

You are calling the processInfo() function twice: once on the form submit event, and once on the onclick on the input. 您正在调用processInfo()函数两次:一次在表单提交事件上,一次在onclick上输入。

You should only attach the processInfo() function on the submit event. 您应该只在submit事件上附加processInfo()函数。 Remove the onlick dom0 event handler (inline scripts are to be avoided). 删除onlick dom0事件处理程序(应避免使用内联脚本)。

Also, do not use return false; 另外,不要使用return false; as it prevents event bubbling. 因为它可以防止事件冒泡。 Use ev.preventDefault() instead. 请改用ev.preventDefault()。

$("document").ready(function() {
    $("#notify").submit(function(ev) {
        ev.preventDefault();
        processInfo();
    });
});

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

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