首页 > 解决方案 > 如果特定字段为空,则不应打开或关闭拨动开关

问题描述

当有人尝试验证客户并且特定字段为空时,例如“分类”,因此切换按钮应显示警告消息“请先填写分类”而不是:按钮验证工作正常。

这是我的代码

   $("#dataTableBuilder_wrapper").on("change", ".verifycus", function () {
        let verified = $(this).is(":checked") ? true : false;
        $.ajax({
            type: "PATCH",
            url: "customers/" + $(this).attr("dataId"),
            data: {verified: verified},
            success: function (data) {
                if (data.verifycus) {
                    if (verified)
                        responseAlert({type: 'success', title: '@lang('main.customer') @lang('sweetalert.verified')'})
                    else
                        responseAlert({type: 'success', title: '@lang('main.customer') @lang('sweetalert.unverified') '})
                }
            },
            error: function (data) {
                responseAlert({type: 'error', title: '@lang('main.customerverificationfailed')'})
            }
        });
    });

我应该怎么办??

标签: jqueryajaxlaravel

解决方案


在发送 ajax 调用之前添加这样的条件。注意:我假设您的分类字段具有“分类”作为它的 ID,您必须根据您的代码更改 ID。

$("#dataTableBuilder_wrapper").on("change", ".verifycus", function () {
    let verified = $(this).is(":checked") ? true : false;
    // Use your id in place of classification
    if($('#classification').val() == '' || $('#classification').val() == undefinded ){
        alert('please fill the classification field');
    }
    $.ajax({
        type: "PATCH",
        url: "customers/" + $(this).attr("dataId"),
        data: {verified: verified},
        success: function (data) {
            if (data.verifycus) {
                if (verified)
                    responseAlert({type: 'success', title: '@lang('main.customer') @lang('sweetalert.verified')'})
                else
                    responseAlert({type: 'success', title: '@lang('main.customer') @lang('sweetalert.unverified') '})
            }
        },
        error: function (data) {
            responseAlert({type: 'error', title: '@lang('main.customerverificationfailed')'})
        }
    });
});

推荐阅读