简体   繁体   English

在选择列表上使用jQuery验证

[英]Using jQuery validate on select list

I have the following javascript: 我有以下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;
            }

        });

This is successfully validating all my input fields but upon trying to apply a similar method to the select type using code: 这可以成功验证我的所有输入字段,但是在尝试使用代码将类似方法应用于选择类型时:

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

My HTML is as follows: 我的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>

How can I ensure that a TariffType value is selected using this method? 如何确保使用此方法选择了TariffType值?

改用.valid()方法...

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

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

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