更改选择选项

时间:2015-09-01 19:42:51

标签: jquery asp.net-mvc-4 jquery-validate unobtrusive-validation

我有一对依赖字段 - state和zipcode,以及一个api控制器,用于验证邮政编码是否出现在状态中。我可以更改State下拉列表以再次验证邮政编码 - 但是,如果我更改了选择后,则不会进行验证!

页面中重要的部分:

@Html.LabelFor(model => model.State, new { @class = "control-label col-md-1" })
<div class="col-md-2">
    @Html.DropDownListFor(model => model.State, Model.StateSelections, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.State, "", new { @class = "text-danger" })
</div>

@Html.LabelFor(model => model.Zip, new { @class = "col-md-1 control-label" })
<div class="col-md-2">
    @Html.EditorFor(model => model.Zip, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Zip, "", new { @class = "text-danger" })
</div>

jquery:

$(document).ready(function () {

    $("#@Html.IdFor(model => model.State)").on("change", function () {
        $("#@Html.IdFor(model => model.Zip)").valid();
    });


    $("#@Html.IdFor(model => model.Zip)").rules("add", {
        "remote": {
            url: zipcodeApiUrl,
            type: "GET",
            data: {
                stateAbbreviation: function () {
                    //alert('I validate, but only the first time');
                    return $("#@Html.IdFor(model => model.State)").val();
                }
            }
        }
    });
});

这是呈现的代码:

var stateId = '#State';
var zipcodeId = '#Zip';
$(document).ready(function () {

    $(stateId).on("change", function () {
        $(zipcodeId).valid();
    });


    $(zipcodeId).rules("add", {
        "remote": {
            url: zipcodeApiUrl,
            type: "GET",
            data: {
                stateAbbreviation: function () {
                    return $(stateId).val();
                }
            }
        }
    });

});

(剪断)

<label class="control-label col-md-1" for="State">State</label>
<div class="col-md-2">
    <select data-val="true" data-val-required="The State field is required." htmlAttributes="{ class = form-control }" id="State" name="State">
        <option value="AK">AK - ALASKA</option>
        <option value="AL">AL - ALABAMA</option>
        <option value="AP">AP - ARMED FORCES PACIFIC</option>
        (...)
        <option value="WI">WI - WISCONSIN</option>
        <option value="WV">WV - WEST VIRGINIA</option>
        <option value="WY">WY - WYOMING</option>
        </select>
    <span class="field-validation-valid text-danger" data-valmsg-for="State" data-valmsg-replace="true"></span>
</div>

<label class="col-md-1 control-label" for="Zip">Zip</label>
<div class="col-md-2">
    <input class="form-control text-box single-line" data-val="true" data-val-length="The field Zip must be a string with a maximum length of 5." data-val-length-max="5" data-val-regex="Zip code must be a 5 digit number" data-val-regex-pattern="\d{1,5}" data-val-required="The Zip field is required." id="Zip" name="Zip" type="text" value="11735" />
    <span class="field-validation-valid text-danger" data-valmsg-for="Zip" data-valmsg-replace="true"></span>
</div>

编辑:奇怪的是,如果我删除了zipcode字段中的字符,验证将再次开始工作,直到我与下拉列表进行交互。也许有一个数据属性需要更改才能再次调用它?

试图找出为什么有效()只能运作一次。

1 个答案:

答案 0 :(得分:0)

我挖了很远,发现导致问题的确切LoC并发布到了回购。它似乎真的打算以这种方式执行!

http://github.com/jzaefferer/jquery-validation/issues/1567

我有一个拉入请求合并到jquery-validate中修复了这个错误!