下拉列表:
<div class="col-md-2">
<div class="form-group">
<label for="field-3" class="control-label">Priority</label>
<select id="lstpriority" class="selectpicker" data-live-search="true" data-style="btn-white">
</select>
</div>
</div>
Jquery Validate:
$("#frmAddEdit").validate({
lstpriority: {
required: {
depends: function (element) {
return $("#lstpriority").val() == "";
}
}
},
messages: {
lstpriority: {
required: "Please select an option from the list",
},
},
submitHandler: function (form) {
abc();
return false;
}
});
下拉绑定:
function bindPrioritytype() {
$.ajax({
dataType: "json",
type: "POST",
url: 'AdminWeb.asmx/GetLOV',
data: 'id=0&type=PRIORITYTYPELOV',
async: true,
success: function (data) {
$("#lstpriority").append("").val("").html("");
$("#lstpriority").append($("<option></option>").val("").html("Select"));
$.each(data, function (key, val) {
$("#lstpriority").append($("<option></option>").val(val.TB_ID).html(val.TB_NAME));
});
$('#lstpriority').selectpicker('refresh');
}
});
}
即使我也尝试添加所需的类,我也无法在提交时验证下拉列表。我想验证下拉列表,请帮助我解决此问题。
尝试使用
lstpriority: {required: true}
而不是
lstpriority: {
required: {
depends: function (element) {
return $("#lstpriority").val() == "";
}
}
},
使用这段代码。您还可以声明一些默认选择,例如 Choose。或选择和valueNotEquals: ""
,您可以将其更改为valueNotEquals: "Select"
完整代码
// configure your validation on a list
$("#frmAddEdit").validate({
rules: {
lstpriority: { valueNotEquals: "" }
},
messages: {
lstpriority: { valueNotEquals: "Please select an item!" }
}
});