jQuery的/ Ajax请求被发送两次 [英] jQuery/Ajax request is being sent twice

查看:382
本文介绍了jQuery的/ Ajax请求被发送两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经扫描我的code一遍又一遍,但我似乎无法发现问题。 当我点击链接#增加用户-BTN 文件 actions.php 被称为两次(并因此PHP脚本执行两次)。

下面的脚本:?我想这事做的JavaScript中的Ajax请求的前

  $(函数(){
    $(#附加用户BTN)。点击(函数(五){
        即preventDefault();
        变种的电子邮件= $(#邮件)VAL()。
        变种名称= $(#NAME)VAL()。
        VAR行动=的adduser;

        $阿贾克斯({
            网址:../actions.php,
            键入:POST,
            数据: {
                动作:动作,
                电子邮件:电子邮件,
                名称:名称,
            },
            数据类型:JSON,
            成功:功能(数据){
                $(近距离揭示模态)点击();
            }
        });
    });
});
 

中的HTML:

 < D​​IV ID =的adduser级=显示峰>
 < H1>添加新用户和LT; / H1>
 < P><标签=电子邮件>电子邮箱:LT; /标签> <输入ID =电子邮件NAME =电子邮件类型=文本占位符=name@example.com/>< / P>
 < P><标签=名与GT;名称:< /标签> <输入ID =名的名称=名称类型=文本占位符=亚当·斯密/>< / P>
 < P><标签=密码>密码:LT; /标签> <输入ID =密码NAME =密码类型=密码占位符=123456/>< / P>
 &其中p为H.;
 <标签=授权>授权:LT; /标签>
 <选择一个id =授权NAME =授权>
    <期权价值=0>管理员< /选项>
    <期权价值=1>超级用户< /选项>
    <期权价值=2>用户< /选项>
 < /选择>
 &所述; / P>
 <按钮ID =添加用户-BTN>提交< /按钮>
 <一类=近距离揭示模态>&安培;#215;< / A>
 < / DIV>
 

解决方案

尝试

  $(#附加用户BTN)解除(点击)绑定(点击,函数(){});
 

这将确保click事件只被调用一次。

I've been scanning my code over and over again but I can't seem to find the problem. When I click the link #add-user-btn the file actions.php is called twice (and hence the PHP script is executed twice).

Here's the script: I suppose it has something to do with the javascript in front of the ajax request?

$(function () {
    $("#add-user-btn").click(function (e) {
        e.preventDefault();
        var email = $("#email").val();
        var name = $("#name").val();
        var action = "adduser";

        $.ajax({
            url: '../actions.php',
            type: 'POST',
            data: {
                action: action,
                email: email,
                name: name,
            },
            dataType: 'json',
            success: function (data) {
                $(".close-reveal-modal").click();
            }
        });
    });
});

The HTML:

<div id="adduser" class="reveal-modal">
 <h1>Add new user</h1>
 <p><label for="email">E-mail:</label> <input id="email" name="email" type="text" placeholder="name@example.com" /></p>
 <p><label for="name">Name:</label> <input id="name" name="name" type="text" placeholder="Adam Smith"/></p>
 <p><label for="password">Password:</label> <input id="password" name="password" type="password" placeholder="123456" /></p>
 <p>
 <label for="authorization">Authorization:</label> 
 <select id="authorization" name="authorization">
    <option value="0">Administrator</option>
    <option value="1">Superuser</option>
    <option value="2">User</option>
 </select>     
 </p>
 <button id="add-user-btn">Submit</button>
 <a class="close-reveal-modal">&#215;</a>
 </div>

解决方案

Try

$("#add-user-btn").unbind('click').bind('click', function () { });  

This will ensure the click event only gets called once.

这篇关于jQuery的/ Ajax请求被发送两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆