简要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">×</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
每次传完值,清空一下,怎么样
我感觉是模态窗的问题,第二次点击时,虽然只点了一下,但是实际是发生了二次点击。
解决方法:$('.btnCheckOk').off('click');