JQuery Validate Options not working MVC3 Razor

旧时模样 提交于 2019-12-11 06:50:06

问题


I am trying to get a simple ignore working for the JQuery validate and I dont seem to be able to get it to work. No matter what I do it seems to not use the options. I have trimmed the code down and pasted below. Thanks for any help.

I am using MVC3 Razor and JQuery 1.5.1

@Code
    ViewData("Title") = "Index"
    Layout = "~/Areas/Quote/Views/Shared/_Layout.vbhtml"
End Code
<script type="text/javascript">
    $(document).ready(function () {
        $('#myform').validate({
            ignore: "#textx"
        })
    });
</script>
@Using Html.BeginForm("Index", "YourQuote", FormMethod.Post, New With {.id = "myform"})
    @<input  type="text" id="textx" class="required"/>
    @<input type="submit" value="save" />
End Using

Thanks for the reply. The example is simplified but what I really want to do is have an overriding rule which says ignore all hidden elements from validation. My page is dynamically built and removing class and adding them back in when parent elements are hidden or shown is not good for me

@Code
    ViewData("Title") = "Index"
    Layout = "~/Areas/Quote/Views/Shared/_Layout.vbhtml"
End Code
<script type="text/javascript">
    $(document).ready(function () {
        $('#myform').validate({
            ignore: ":hidden"
        })
    });
</script>
@Using Html.BeginForm("Index", "YourQuote", FormMethod.Post, New With {.id = "myform"})
    @<div style="display:none">
    <input  type="text" id="textx" class="required"/>
    </div>
    @<input type="submit" value="save" />
End Using

回答1:


Try removing the unobtrusive jquery inclusion, and just include jquery.validate. I had a similar problem and somehow the unobtrusive library screwed up my custom defined settings




回答2:


Is the required class conflicting with the ignore settings? Try removing the class when you want to ignore that field (i.e. if you are conditionally ignoring the field, you will need to use .removeClass() in a callback or similar at the appropriate point).



来源:https://stackoverflow.com/questions/8003679/jquery-validate-options-not-working-mvc3-razor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!