jQuery ajax在1次点击时发送多个请求

时间:2015-04-01 10:07:20

标签: jquery ajax laravel

我不知道我的代码发生了什么我点击发送按钮发送电子邮件并发送多个重复的电子邮件..相同的jQuery和html代码用于另一个页面删除文件和工作正常.. 我在做什么吗?错了? enter image description here

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:

<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>

8 个答案:

答案 0 :(得分:2)

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

将起作用

答案 1 :(得分:2)

对我来说,帮助是在调用方法后添加e.stopImmediatePropagation()

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

答案 2 :(得分:2)

$(document).on('click', '.sendwork',function(event) {
    event.stopImmediatePropagation();
    // this will help
});

$("#form").on('submit' ,function(event) {
    event.stopImmediatePropagation();
    // this help me in same issue
}

答案 3 :(得分:1)

问题解决了我刚刚更改了代码:

表格

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

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

答案 4 :(得分:1)

试试这个

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

答案 5 :(得分:1)

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

此处的解决方案:http://eligeske.com/jquery/jquery-sending-multiple-ajax-requests-all-by-itself-kind-of/

答案 6 :(得分:0)

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

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

答案 7 :(得分:0)

使用 async:false 阻止多个请求

import pandas as pd

df = pd.DataFrame(index=['Q1-2013',
 'Q1-2014',
 'Q1-2015',
 'Q1-2016',
 'Q1-2017',
 'Q1-2018',
 'Q2-2013',
 'Q2-2014',
 'Q2-2015',
 'Q2-2016',
 'Q2-2017',
 'Q2-2018',
 'Q3-2013',
 'Q3-2014',
 'Q3-2015',
 'Q3-2016',
 'Q3-2017',
 'Q3-2018',
 'Q4-2013',
 'Q4-2014',
 'Q4-2015',
 'Q4-2016',
 'Q4-2017',
 'Q4-2018'])

#    df_new = pd.DataFrame(index=pd.to_datetime(['-'.join(s.split('-')[::-1]) for s in df.index]))    
    df_new = pd.DataFrame(index=pd.to_datetime(['-'.join(s.split('-')[::-1]) for s in df.index]).to_period('M'))
#    df_new = pd.DataFrame(index=pd.to_datetime(['-'.join(s.split('-')[::-1]) for s in df.index]).to_period('M').to_timestamp().strftime('m-%Y'))


print(df_new.index)

PeriodIndex(['2013-01', '2014-01', '2015-01', '2016-01', '2017-01', '2018-01',
             '2013-04', '2014-04', '2015-04', '2016-04', '2017-04', '2018-04',
             '2013-07', '2014-07', '2015-07', '2016-07', '2017-07', '2018-07',
             '2013-10', '2014-10', '2015-10', '2016-10', '2017-10', '2018-10'],
            dtype='period[M]', freq='M')