在调用DestroyeAll函数之前,检查是否选中了复选框

时间:2016-11-11 04:38:33

标签: jquery ajax checkbox

如果没有选中复选框,则应显示警告没有选择记录如果选中复选框,则应转至控制器,否则只需在复选前提醒复选框 这是我的方法

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";
                }
            }
        });
    }
}

我想在这里添加新的东西,如果没有复选框,它应该显示警告不应该在网址上

2 个答案:

答案 0 :(得分:2)

试试这个:

  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";
                    }
                }
        });
    }
}

答案 1 :(得分:0)

你可以尝试这个

function delete(){

if($('input[type=checkbox]:checked').length != 0){
            if(confirm('Are you SURE!')) {
        var checkedValues = $('input[type=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";
                        }
                    }
});
}else{
alert('no checkbox checked')
}

}