EasyUI Combobox 內建非空驗證僅針對無選項,若第一項為「請選擇」需自行判斷並觸發提示。
if ($('#state').combobox('getText') == '請選擇') {
alert('此項必填');
return false; // 阻止表單提交
}
$('#myForm').form('validate'); // 先跑內建驗證
var txt = $('#state').combobox('getText');
if (txt == '請選擇' || txt == '') {
// 手動顯示 EasyUI 提示框
$('#state').combobox('textbox').focus();
$.messager.show({
title: '提示',
msg: '此項必填',
timeout: 2000,
showType: 'slide'
});
return false;
}
// 通過驗證,繼續提交
$.extend($.fn.validatebox.defaults.rules, {
comboboxRequired: {
validator: function(value, param) {
var text = $(param[0]).combobox('getText');
return text != '請選擇' && text != '';
},
message: '此項必填'
}
});
// 套用
<input id="state" class="easyui-combobox" data-options="validType:'comboboxRequired[#state]'" />
如此可複用且符合 EasyUI 驗證體系。