Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery验证-如果选中复选框,Textbox需要数据_Jquery_Validation_Asp.net Mvc 4_Jquery Validate - Fatal编程技术网

jQuery验证-如果选中复选框,Textbox需要数据

jQuery验证-如果选中复选框,Textbox需要数据,jquery,validation,asp.net-mvc-4,jquery-validate,Jquery,Validation,Asp.net Mvc 4,Jquery Validate,我有一个复选框-HasRaffle,如果选中了HasRaffle,则需要一个文本框-RaffleItem来包含数据。我该怎么做?我以前从未做过自己的jQuery验证。这是我尝试的,但根本不起作用。我离你很近吗 $("#donationEventForm").validate({ rules: { RaffleItem: { required: function () { if ($("#HasRaffle").is(

我有一个复选框-
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.BeginForm(“Create”、“DonationEvent”、FormMethod.Post、new{id=“donationEventForm”})){
@Html.AntiForgeryToken()
@Html.ValidationSummary(false)
@LabelFor(model=>model.Charity)
@Html.TextBoxFor(model=>model.Charity)
@Html.ValidationMessageFor(model=>model.Charity)
@LabelFor(model=>model.StartDate)
@Html.TextBoxFor(model=>model.StartDate,new{@class=“datepicker”})
@Html.ValidationMessageFor(model=>model.StartDate)
@LabelFor(model=>model.EndDate)
@TextBoxFor(m=>m.EndDate,新的{@class=“datepicker”})
@Html.ValidationMessageFor(model=>model.EndDate)
@Html.Label(“抽奖?”)
@复选框(“HasRaffle”,true)
@LabelFor(model=>model.RaffleItem)
@Html.TextBoxFor(model=>model.RaffleItem)
@Html.ValidationMessageFor(model=>model.RaffleItem)
@Html.TextBoxFor(model=>model.GLCode,新的{@type=“hidden”})
@Html.TextBoxFor(model=>model.TransactionDescription,新的{@type=“hidden”})
@Html.TextBoxFor(model=>model.CreatedBy,new{@type=“hidden”})
}

您需要使用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,所以可能无法工作),但是如果您能看到我的代码背后的逻辑,您可能可以调试您的代码

这只是图片的一半。还显示表单的HTML。