首页 > 解决方案 > 仍然没有选择任何内容时对组合框进行验证

问题描述

当没有选择触发方法时,我试图让 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() });

标签: jquery

解决方案


推荐阅读