【问题标题】:Jquery Unobtrusive Validation 'Cannot read property 'type' of undefined'Jquery Unobtrusive Validation'无法读取未定义的属性'类型''
【发布时间】:2015-12-29 23:12:19
【问题描述】:

我在我的 MVC 应用程序中使用 jQuery Unobtrusive Validation。发回时,验证按规定工作。但是,我有正当理由阻止回发客户端。

我遵循在多个站点上看到的规定方法...并且我尝试了许多不同的组合。但是,我仍然不断收到以下错误:

"Cannot read property 'type' of undefined"

我的 JAVASCRIPT 看起来像:
当我调用“showErrors”时会发生错误...

function onValidateCity(e){

    // Checks the value of my Kendo Combobox
    var value = this.value();
    if (value && this.selectedIndex === -1) {

        // Yes, it reaches this part
        var validator = $('#CityId').validate();
        validator.showErrors({"CityId": "My custom error message!"});
    }
}

我的 MVC 助手创建以下 HTML:

<span class="k-widget k-combobox k-header form-control">
    <span tabindex="-1" unselectable="on" class="k-dropdown-wrap k-state-default">
        <input name="CityId_input" class="k-input form-control" type="text" autocomplete="off" title="" maxlength="524288" role="combobox" aria-expanded="false" placeholder="-- Select City --" tabindex="0" aria-disabled="false" aria-readonly="false" aria-autocomplete="list" aria-owns="CityId_listbox" aria-busy="false" style="width: 100%;" /> 
        <span tabindex="-1" unselectable="on" class="k-select">
            <span unselectable="on" class="k-icon k-i-arrow-s" role="button" tabindex="-1" aria-controls="CityId_listbox">select</span>
        </span>
    </span>
    <input autocomplete="off" class="form-control" data-val="true" data-val-required="The City field is required." id="CityId" name="CityId" type="text" value=  "28dbddea-660d-4bbb-b063-f926b1153866" data-role="combobox" aria-disabled="false"
aria-readonly="false" novalidate="novalidate" style="display: none;" />
</span>

<span class="field-validation-valid text-danger" data-valmsg-for="CityId" data-valmsg-replace="true"></span>

MVC(验证)助手看起来像:
@Html.ValidationMessageFor(model => model.CityId, "", new { @class= "text-danger" })

【问题讨论】:

  • 乍一看你没有id="CityId"的元素。看起来您的验证器根本没有初始化。将id="CityId" 添加到您的input 元素中。
  • 查看“data-val-required”属性...谢谢
  • 抱歉,没有看到第二个输入。触摸。试图在小提琴中重现问题。
  • 感谢您的帮助:)

标签: javascript jquery jquery-validate unobtrusive-validation


【解决方案1】:

当您初始化validator 时,请针对嵌套在其中的包含CityId 输入的元素执行此操作,而不是针对输入元素本身。它可以是 formdivspan

这是在小提琴中工作的简化代码,我是针对span 做的: http://jsfiddle.net/o0qhcbbL/1/

$('#button').click(function (e) {
    onValidateCity(e)
})

function onValidateCity(e) {

    var validator = $('#xx').validate();

    validator.showErrors({
        "CityId": "My custom error message!"
    });
}

html:

<span id="xx">
    <input id="CityId" name="CityId" type="text" />
</span>
<span id="button">select</span>

【讨论】:

  • 我在之前的尝试中确实尝试过,但它只是将消息“闪烁”到屏幕上......然后......从“显示元素”中清空消息。事实上,它的速度非常快,除非你在 javascript 中设置断点,否则你甚至看不到消息。
  • 您永远不会将.validate() 方法附加到inputdivspan.validate() 方法仅附加到 form 元素,并且已由 Unobtrusive Validation 插件自动完成。
  • 好的...酷...我现在明白了...谢谢。那么...为什么消息在 JavaScript 函数完成的那一刻就消失了?
猜你喜欢
  • 1970-01-01
  • 2021-11-20
  • 2019-06-03
  • 2017-02-08
  • 2021-01-15
  • 2020-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多