recovery men recovery men - 28 days ago 6
Ajax Question

Check if No checkbox checked before calling DestroyeAll Function

If no checkbox is selected an alert should be show no Record Selected if checkboxes checked it should then go to Controller otherwise just alert to checkbox before Delete
Here is my method

function delete () {
if(confirm('Are you SURE!')) {
var checkedValues = $('input:checkbox:checked').map(function() {
return this.value;
}).get();
$.ajax({
type : "POST",
url : "{{ url('/school/destroyAll') }}",
data : {ids: checkedValues, _token: "{{ csrf_token() }}"},
success: function(result) {
if(result == 'delete') {
window.location.href = "/school";
}
}
});
}
}


Just I want to add new Thing here if no checkbox sleeted it should show an alert no should go on url

Answer

Try this :

  function delete () {
       if(!$('input:checkbox:checked').length){
           alert("No checkbox has selected");
           return false;
        }
       if(confirm('Are you SURE!')) {
           var checkedValues = $('input:checkbox:checked').map(function() {
           return this.value;
           }).get();
          $.ajax({
                type    : "POST",
                url     : "{{ url('/school/destroyAll') }}",
                data    : {ids: checkedValues, _token: "{{ csrf_token() }}"},
                success: function(result) {
                                if(result == 'delete') {
                        window.location.href = "/school";
                    }
                }
        });
    }
}