laravel jQuery ajax 一键发送多个请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29388002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:16:18  来源:igfitidea点击:

jQuery ajax sending multiple request on 1 click

jqueryajaxlaravel

提问by Muhammad Hamza Younas

i don't know what happened to my code i am clicking on send button send email and its sending multiple duplicate emails.. Same jQuery and html code used in another page for delete files and working fine.. is i am doing some thing? wrong ? enter image description here

我不知道我的代码发生了什么我点击发送按钮发送电子邮件并发送多个重复的电子邮件..在另一个页面中使用相同的 jQuery 和 html 代码删除文件并且工作正常..我在做些什么吗?错误的 ? 在此处输入图片说明

jQuery:

jQuery:

$(document).on('click','.sendwork',function(e){
    $('#spinner').show();
    var sendWork = $(this).data('id');

    var res         = sendWork.split("-");
    var comment_id  =  res[0];
    var status      =  res[1];
    var order_id    =  res[2];
    var td = $(this).closest("td").andSelf();

    $.ajax({
        type:'POST',
        url:'{!! URL::to('admin/sendwork/') !!}',
        cache: false,
        data:{
         '_token'   : '{{ csrf_token() }}',
         'comment_id':comment_id,
         'status':status,
         order_id:order_id,
        },
        success: function(data){
            $('#spinner').hide();
            if (data == 'true') {
                // $('.email-resp').html('');
                td.html('Email Sent');
            }

        },
        error:function(data){
            $('#spinner').hide();
            td.html('<p style="color:red; font-weight:bold;">Fail</p>');
        }
    });

 });

html:

html:

<table class="table table-compact table-bordered">

    <tr>
        <th width="10"></th>
        <th width="30">Date</th>
        <th width="20">User</th>
        <th width="20">Status</th>
        <th width="450">Comment</th>
        <th width="100">Files</th>
    <th width="30">Action</th>
    </tr>

    <tr>
        <td><a class="del-comment" href="javascript:void(0)" data-token="XyjbZEsvbfTnurM0OnRP75k049Re0dPpLynRqUe6" id="217"><span style="color:red;" class="glyphicon glyphicon-remove"></span></a> </td>
        <td>01-Apr-15</td>
        <td width="30">hy</td>
        <td>Completed</td>
        <td width="42" align="left"><p>sdf</p></td>
        <td>
            <a href="/uploads/PE10_1427882386_0.png" target="_blank" alt="PE10_1427882386_0.png" title="PE10_1427882386_0.png"><i class="fa fa-picture-o"></i></a>                  </td>
        <td><a href="javascript:void(0)" class="btn btn-small btn-success sendwork" data-id="217-Completed-LEH1000">Send</a></td>
    </tr>

</table>

采纳答案by hy_sultani

try this

尝试这个

$(document).on('click','.sendwork',function(e){
   // do some thing
});

回答by Tiger

$(document).off('click').on('click', function(){
   //
});

will work

将工作

回答by Muhammad Hamza Younas

Problem solved i just changed the code:

问题解决了我只是改变了代码:

form

形式

$(document).on('click','.sendwork',function(e){

to

$('.sendwork').on('click',function(e){

回答by Mizael Clistion

" I solved my issue adding $(‘#foo').unbind(‘click'); in .done(function(data) event attached with $.ajax. Thank You!"

“我解决了添加 $('#foo').unbind('click'); 在 $.ajax 附带的 .done(function(data) 事件中的问题。谢谢!“

Solution here: http://eligeske.com/jquery/jquery-sending-multiple-ajax-requests-all-by-itself-kind-of/

解决方案在这里:http: //eligeske.com/jquery/jquery-sending-multiple-ajax-requests-all-by-itself-kind-of/

回答by Awais Jameel

This code really works great for me.

这段代码对我来说真的很棒。

$('.class').unbind().bind('eventName', method);

回答by RahmanRezaee

use async:falseto prevent multiple request

使用async:false防止多个请求

$(document).off('click').on('click', function(){
    $.ajax({
        type:'POST',
        url: ,
        async:false,
        cache: false,
        data:{}
         ,
        success: function(data){

        },
        error:function(data){
        }
    });   

});