简体   繁体   English

带下拉菜单的MVC3条件Jquery验证

[英]MVC3 Conditional Jquery Validation w/ Dropdown

I been trying to do some jquery validation where a textfield or dropdown is validated only if a checkbox is checked. 我一直在尝试进行一些jquery验证,仅在选中复选框的情况下才验证文本字段或下拉列表。

Here is what I have: 这是我所拥有的:

@using (Ajax.BeginForm("SomeAction", "SomeController", new { id = Model.Id }, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "somePanel", InsertionMode = InsertionMode.Replace }, new { @id = "testForm" }))
{
 @Html.DropDownListFor(x => Model.SelectedValue, Model.YesNoList, " ", new { @id ="drpYesNo" })
 @Html.TextAreaFor(x => x.CriminalText, new { @id = "txtSomeField" })

<input type="submit" value="Save" />
}

In the javascript I have this: 在JavaScript中,我有这个:

<script type="text/javascript">
    jQuery.validator.messages.required = "";
    $("#testForm").validate(
    {
        rules: {
            txtSomeField: {
                required: function (element) {
                    return $("input:checkbox[name='drpYesNo']:checked").val() == 'Yes';
                }
            }
        }

    });

    $("#testForm").on("submit", function () {
        if (!$(this).valid()) {
            return false;
        }
    });
</script>

I am following some of the example that are online, but can't seem to work. 我正在跟踪一些在线示例,但似乎无法正常工作。 Any help is much appreciated. 任何帮助深表感谢。 Happy Fourth of July! 七月四日快乐!

When you define a jquery validator rule you should use the name and not the id of the element you are defining the rule for. 定义jquery验证程序规则时,应使用名称而不是要为其定义规则的元素的id So in your case the name of your <textarea> is CriminalText and not txtSomeField (Obviously if this is inside an editor template the name might be different, such as for example Foo.Bar[3].CriminalText ). 所以你的情况你的名字<textarea>CriminalText而不是txtSomeField (显然,如果这是一个编辑器模板的名称可能是不同的,例如内部Foo.Bar[3].CriminalText )。

So the first step is to use the proper name: 因此,第一步是使用适当的名称:

rules: {
    CriminalText: {
        required: function (element) {
            ...
        }
    }
}

Now let's see the second step. 现在让我们看第二步。 In your selector you used some :checkbox that I cannot see anywhere in your DOM. 在选择器中,您使用了:checkbox ,我无法在您的DOM中看到任何地方。 All I can see (at least in what you have shown here) is a DropDownList and a textarea. 我所看到的(至少在您在此处显示的内容中)是一个DropDownList和一个textarea。 So if you want the <textarea> to be required only if the user selected the value Yes inside the <select> list, here's how you could define the rule: 因此,如果仅当用户在<select>列表中<select>值为“ Yes时才需要<textarea> ,则可以使用以下方法定义规则:

rules: {
    CriminalText: {
        required: function (element) {
            return $('#drpYesNo').val() == 'Yes';
        }
    }
}

This obviously assumes that you have the following option selected: 显然,这假定您选择了以下选项:

<select>
    <option value="Yes">Yes, of course</option>
    ...
</select>

And one final remark: make sure that you have not included the jquery.validate.unobtrusive.js script to your page as it will use Microsoft's unobtrusive library to automatically write the jQuery.validate rules based on the HTML5 data-* attributes generated by the helpers and thus completely messing up your custom rule setup. 最后一点:确保您没有在页面中包含jquery.validate.unobtrusive.js脚本,因为它将使用Microsoft的unobtrusive库自动根据jQuery生成的HTML5 data- *属性编写jQuery.validate规则。帮助程序,从而完全弄乱了您的自定义规则设置。

The selector here is the problem, using input:checkbox is for checkboxes. 这里的选择器是问题所在,使用input:checkbox用于复选框。 Typically, jquery prefers css class names or id's. 通常,jQuery倾向于使用CSS类名或ID。

required: function (element) {
  return $("#drpYesNo").val() === 'Yes';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM