jquery - 如果特定字段为空,则不应打开或关闭拨动开关
问题描述
当有人尝试验证客户并且特定字段为空时,例如“分类”,因此切换按钮应显示警告消息“请先填写分类”而不是:按钮验证工作正常。
这是我的代码
$("#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')'})
}
});
});
我应该怎么办??
解决方案
在发送 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')'})
}
});
});
推荐阅读
- android - 片段未附加到截击响应的活动
- httprequest - 数量被追加而不是被添加到 POST API 标签中
- asp.net-mvc - 如何修复 mvc 错误,如描述中所示
- javascript - Javascript - 在 ID 上选中特定单选按钮时显示警报
- jbpm - 无法在 OptaPlanner 快速入门中选择“规划实体集合”
- ios - pod install 后 iOS 通用框架编译器版本发生变化
- reactjs - react-date-picker 如何在原始输入上自动更正
- c++ - 如何使 QLineSeries/QXYSeries 仅显示一个点标签
- c# - 带有身份的 Asp.Net Core 3.0 Angular SPA 模板使用身份服务器主页 User.Identity 为空
- javascript - Angular 8:组件内部的formControlName下面有多个嵌套级别