首页 新闻 会员 周边 捐助

ajax 第二次点击怎么会把上次点击的传再传进去一次?

0
悬赏园豆:15 [已关闭问题] 关闭于 2018-03-17 17:27

简要HTML,三条数据,一个bootstrap的模态窗:

<button type="button" class="btn btn-primary btn-sm btnPayment" data-id="112" data-target="#myModal">付款</button>
<button type="button" class="btn btn-primary btn-sm btnPayment" data-id="111" data-target="#myModal">付款</button>
<button type="button" class="btn btn-primary btn-sm btnPayment" data-id="106" data-target="#myModal">付款</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">审核意见</h4>
            </div>
            <div class="modal-body" id="myModalbody">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">不通过</button>
                <button type="button" class="btn btn-primary btnCheckOk">通过</button>
            </div>
        </div>
    </div>
</div>

JS:

 $('.btnPayment').on('click', function () {
                $('#myModal').modal('show')
                var $that = $(this);
                $('#myModalLabel').text('付款');
                var id = $that.data('id');
                var userid = $that.parent().siblings().first().html().trim();
                var amount = $that.parent().siblings().eq(1).html().trim();
                $('#myModalbody').text('确定向用户 ' + userid + ' 支付¥: ' + amount + ' 吗?');
                $('.btn-default').text('取消');
                $('.btnCheckOk').text('确定');
                $('.btnCheckOk').on('click', function () {
                    $.ajax({
                        url: '/Manager/Admin/PaymentToAlipay',
                        datatype: 'JSON',
                        type: 'POST',
                        data: { 'id': id, 'userid': userid, 'amount': amount },
                        success: function (data) {
                            layer.msg(data.msg);
                            if (data.status == '1') {
                                //window.location = window.location;
                                $('#myModal').modal('hide')
                                $that.parent().parent().remove();
                                $('#myModalbody').text('');
                            }
                        }
                    });
                });
                
            });

现象:

例如首先点击  data-id="112" ,返回正常,layer.msg执行一次;

然后再点击 data-id="111"时,会先把data-id="112"传进去,然后再把data-id="111"传进去,造成会连续执行二次layer.msg

 

DZCMS的主页 DZCMS | 初学一级 | 园豆:5
提问于:2018-03-15 20:19
< >
分享
所有回答(2)
0

每次传完值,清空一下,怎么样

但乱红尘熊 | 园豆:773 (小虾三级) | 2018-03-16 10:25

我感觉是模态窗的问题,第二次点击时,虽然只点了一下,但是实际是发生了二次点击。

支持(0) 反对(0) DZCMS | 园豆:5 (初学一级) | 2018-03-16 21:45
0

解决方法:$('.btnCheckOk').off('click');

DZCMS | 园豆:5 (初学一级) | 2018-03-17 17:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册