简体   繁体   English

jQuery ajax在开关切换时执行多次

[英]JQuery ajax executing multiple times on switch toggle

I have a table list of all the outlets, I have a materialize CSS button to activate or deactivate the outlet, 我有所有插座的列表,有一个实现CSS的按钮可以激活或停用插座,

@foreach($outlets as $outlet)
<tr>
    <td>
        <div class="switch m-b-md" data-id="{{$outlet->id}}">
            <label>
                @if($outlet->status == 'active')
                    <input id="agent-status" type="checkbox" checked="">
                @else
                    <input id="agent-status" type="checkbox">
                @endif
                <span class="lever"></span>
            </label>
        </div>
    </td>
</tr>
@endforeach

the issue is when I click on .switch ajax is executing multiple times, if I keep toggling the switch it is executing more than 5 to 10 times 问题是当我单击.switch ajax执行多次时,如果我不断切换该开关,则执行5至10次以上

$(document).on("click", ".switch", function() {

    var outlet_id = $(this).data('id');

    $(".switch").find("input[type=checkbox]").on("change",function() {

        var status = $(this).prop('checked');

        if(status == true) {
            status = "active";
        } else {
            status = "inactive";
        }

        $.ajax ({
            url: '/manager/outlets/'+outlet_id+'/status',
            type: 'POST',              
            data: {"id": outlet_id, "status": status, "_token": '{{ csrf_token() }}'},
            success: function(data)
            {   
                if(data.status==true) {
                    swal("Updated", "Status updated successfully", "success");
                } else if(data.status==false) {
                    swal("Failed", "Fail to update status try again", "error");
                }
            },
            error: function(error)
            {
                swal("Failed", "Fail to update status try again", "error");
            }
        });

    });

});

thank you 谢谢

You need just separate your event listeners and use outlet_id as closure to have access to it from both listeners. 您只需要分开事件侦听器,并使用outlet_id作为闭包即可从两个侦听器访问它。 See below: 见下文:

    var outlet_id;

    $(document).on("click", ".switch", function() {

        outlet_id = $(this).data('id');

    });

    $(".switch").find("input[type=checkbox]").on("change",function() {

        var status = $(this).prop('checked');

        if(status == true) {
            status = "active";
        } else {
            status = "inactive";
        }

        $.ajax ({
            url: '/manager/outlets/'+outlet_id+'/status',
            type: 'POST',
            data: {"id": outlet_id, "status": status, "_token": '{{ csrf_token() }}'},
            success: function(data)
            {
                if(data.status==true) {
                    swal("Updated", "Status updated successfully", "success");
                } else if(data.status==false) {
                    swal("Failed", "Fail to update status try again", "error");
                }
            },
            error: function(error)
            {
                swal("Failed", "Fail to update status try again", "error");
            }
        });

    });

Your loop creates multiple div with the same class switch , when you click on the class, all div with switch class calls for ajax post. 您的循环使用相同的class switch创建多个div,当您单击该类时,所有带有switch类的div都会调用ajax post。 therefore, please id for each of your div in your loop. 因此,请为循环中的每个div分配id

<div class="switch m-b-md" id="div_{{$outlet->id}}" onclick="callAjax('div_{{$outlet->id}}')">

Your ajax : 你的ajax

function callAjax(divid){

   //do your ajax call here.....

}

it is happening because each time a switch is clicked, a listener is added, you shouldn't have this nested listeners, 之所以会这样,是因为每次单击开关时都会添加一个侦听器,所以您不应该使用此嵌套的侦听器,

what i would do is add delegate function for checkbox change on all input type checkbox, i would give these inputs a class to simplify my selection 我要做的是为所有输入类型复选框上的复选框更改添加委托函数,我将为这些输入提供一个类以简化选择

$(".switch").on('change','.CheckBoxClass', function(){
var theid = $(this).parent('.switch').data('id');
var status = $(this).prop('checked');
//add the rest of your code
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM