【问题标题】:jQuery validation - Textbox requires data if checkbox is checkedjQuery 验证 - 如果选中复选框,则文本框需要数据
【发布时间】:2013-05-08 19:38:27
【问题描述】:

我有一个复选框 - HasRaffle,如果选中 HasRaffle,则需要一个文本框 - RaffleItem 来包含数据。我该怎么做?我以前从未做过自己的 jQuery 验证。这是我尝试过的,但它根本不起作用。我什至接近吗?

$("#donationEventForm").validate({
    rules: {
        RaffleItem: {
            required: function () {
                if ($("#HasRaffle").is(":checked")) {
                    if ($("#RaffleItem").val === '') {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            },
            messages: {
                required: "This is a test!!"
            }
        }
    }
});

编辑:这是我的观点

 @using (Html.BeginForm("Create", "DonationEvent", FormMethod.Post, new {id = "donationEventForm"})) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(false)

    <div class="form-field">
        @Html.LabelFor(model => model.Charity)
        @Html.TextBoxFor(model => model.Charity)
        @Html.ValidationMessageFor(model => model.Charity)
    </div>

    <div class="form-field">
        @Html.LabelFor(model => model.StartDate)
        @Html.TextBoxFor(model => model.StartDate, new {@class = "datepicker"})
        @Html.ValidationMessageFor(model => model.StartDate)
    </div>

    <div class="form-field">
        @Html.LabelFor(model => model.EndDate)
        @Html.TextBoxFor(m => m.EndDate, new {@class = "datepicker"})
        @Html.ValidationMessageFor(model => model.EndDate)
    </div>

    <div class="form-field">
        @Html.Label("Raffle?")
        @Html.CheckBox("HasRaffle", true)
    </div>

    <div class="form-field">
        @Html.LabelFor(model => model.RaffleItem)
        @Html.TextBoxFor(model => model.RaffleItem)
        @Html.ValidationMessageFor(model => model.RaffleItem)
    </div>

    @Html.TextBoxFor(model => model.GLCode, new {@type = "hidden"})
    @Html.TextBoxFor(model => model.TransactionDescription, new {@type = "hidden"})
    @Html.TextBoxFor(model => model.CreatedBy, new {@type = "hidden"})

    <div class="form-field-buttons">
        <input type="submit" value="Create" />
        <input type="button" value="Cancel" onclick="location.href='../Home/Index'"/>
    </div>

}

【问题讨论】:

  • 这只是图片的一半。同时显示表单的 HTML。

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


【解决方案1】:

您需要使用 addMethod 添加自定义规则

jQuery.validator.addMethod('checkRaffle', function(value, element){
    if ($("#HasRaffle").is(":checked")) {
        if (value === '') {
            return false;
        } else {
            return true;
        }
    } else {
        return true;
    }
}, 'Please write something')

那么规则看起来像这样:

rules: {
    'RaffleItem': {
        'checkRaffle' : true
    }
}

此代码未经测试(并且可能无法工作,因为我看不到您的 DOM),但是如果您可以看到我的代码背后的逻辑,您可能可以调试您的代码!

【讨论】:

    猜你喜欢
    • 2011-09-28
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 2013-12-24
    相关资源
    最近更新 更多