jquery - 仍然没有选择任何内容时对组合框进行验证
问题描述
当没有选择触发方法时,我试图让 jquery 验证在组合框上工作。因此,您选择了该字段,但您不选择任何内容并跳转到下一个字段。目前,该方法仅在我在组合框中选择某些内容时触发,而不是在选择“无”时触发(请选择)
我在 html 中的组合框是:
@Html.DropDownList("Collection", new SelectList(Model.TableCollectionDevice.Where(x => x.persona == false), "collection_id" , "collection_displayname"), "Please select", new { @CssClass="ddl_collection", @id="ddl_collection", @class = "form-control" })
我认为错误是“请选择”没有像“”这样的值,这就是它没有被触发的原因。我的 jquery 看起来像这样:
$.validator.addMethod("selectComboboxCheck", function (value, element)
{
console.log("Test"); // Just to see if something gets fired
console.log($(element).find(":selected").val());
if ($(element).find(":selected").val() == 0) {
return false;
}
else return true;
},
"Required");
$(document).ready(function () {
$("#form_import").validate({
errorPlacement: function errorPlacement(error, element) { element.before(error); },
rules: {
Collection:{
required : true ,
selectComboboxCheck: true
},
知道即使没有选择任何内容,我如何触发该方法?
谢谢斯蒂芬
编辑:这条线似乎得到了正确的关注:
$("select").blur(function () { $(this).valid() });
解决方案
推荐阅读
- svelte - 让 Routeify 使用 #hash
- ios - 致命错误:索引超出范围:文件 Swift/ContiguousArrayBuffer.swift
- haskell - 为什么 Haskell 不允许两个数据构造函数共享“Const”和其他定义的数据属性?
- c++ - 为什么 C++ 编译成功?
- common-lisp - 删除 nil 不会转到列表中的最后一个元素
- typescript - 在 TypeScript 中返回 Union 的隐式类型
- python - IDLE python3.9中的If语句无法正常运行
- python - 来自单独数据库连接的 peewee 多进程“peewee.OperationalError:磁盘 I/O 错误”
- string - SAS如何根据字符串将观察分成多行?
- javascript - 如果未找到,则 JS 中的条件链接为 false