使用jQuery验证选择列表

Using jQuery validate on select list

本文关键字:列表 选择 验证 jQuery 使用      更新时间:2023-09-26

我有以下javascript:

var $step = $(".wizard-step:visible:last"); // get current step
        var validator = $("#WizardForm").validate(); // obtain validator
        var anyError = false;
        $step.find("input").each(function ()
        {
            if (!validator.element(this)) { // validate every input element inside this step
                anyError = true;
            }
        });

这成功地验证了我所有的输入字段,但在尝试使用类似的方法来选择类型时使用代码:

$step.find("select").each(function () {
    if (!validator.element(this)) { // validate every input element inside this step
        anyError = true;
    }
});

我的HTML如下:

<div class="wizard-step" id="step1" visibility="hidden" style="display: block;">
        <div class="row">
            <div class="col-md-6 column ui-sortable">
                <div class="form-group">
                    <label class="control-label col-md-4" for="Tariff_Type">Tariff Type</label>
                    <div class="col-md-8">
                        <select style="width:100%;height:35px;border-radius:4px;padding-left:10px;" id="TariffType" name="TariffType" class="form-control input required">
                            <option value="">Please Select Tariff Type</option>
                            <option value="keypad account">Say Hello To Budget Extra Discount</option>
                            <option value="bill pay account">Standard 24H</option>
                        </select>
                        <span class="field-validation-valid text-danger" data-valmsg-for="TariffType" data-valmsg-replace="true"></span>
                    </div>
                </div>
            </div>
        </div>
    </div>

如何确保使用此方法选择TariffType值?

试试.valid()方法…

if (! $(this).valid()) { ...