C# 使用jQuery在asp.net mvc razor中设置表单的必需项
我有这个问题:我创建了反馈表单(表单中是问题,然后是结果(1到5的单选按钮)和注释),这个反馈有很多问题,这些问题是从数据库表问题中加载的。我想动态设置,当用户给出的结果少于3个问题时,对这个问题的评论成为必需的。另一方面,当用户给出更好的结果(如mark 3)时,不需要对此问题进行评论 这是我的反馈表:C# 使用jQuery在asp.net mvc razor中设置表单的必需项,c#,jquery,asp.net,asp.net-mvc,razor,C#,Jquery,Asp.net,Asp.net Mvc,Razor,我有这个问题:我创建了反馈表单(表单中是问题,然后是结果(1到5的单选按钮)和注释),这个反馈有很多问题,这些问题是从数据库表问题中加载的。我想动态设置,当用户给出的结果少于3个问题时,对这个问题的评论成为必需的。另一方面,当用户给出更好的结果(如mark 3)时,不需要对此问题进行评论 这是我的反馈表: @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@foreach (var item in Model)
{
<div class="form-group" style="text-align:center;">
<h2> @Html.Label(item.questions.question) </h2>
<div class="col-md-10">
<label class="radio-inline">@Html.RadioButton(item.question_id.ToString(), 1) 1 </label>
<label class="radio-inline">@Html.RadioButton(item.question_id.ToString(), 2) 2 </label>
<label class="radio-inline">@Html.RadioButton(item.question_id.ToString(), 3) 3 </label>
<label class="radio-inline">@Html.RadioButton(item.question_id.ToString(), 4) 4 </label>
<label class="radio-inline">@Html.RadioButton(item.question_id.ToString(), 5) 5 </label>
</div>
<div class="col col-md-10">
<label>comment: @Html.TextArea(item.question_id.ToString() + "_comment", new { @class = "form-control", @cols = 200, @rows=5 }) </label>
</div>
</div>
<hr>
}
<input type="hidden" name="feedback_id" value="@ViewBag.feedback_id">
<div class="form-group">
<div class="col-md-12" style="margin-left:330px;">
<input type="submit" value="Send" class="btn btn-primary btn-block" />
</div>
</div>
</div>
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@foreach(模型中的var项目)
{
@Label(item.questions.question)
@RadioButton(item.question_id.ToString(),1)1
@RadioButton(item.question_id.ToString(),2)2
@RadioButton(item.question_id.ToString(),3)3
@RadioButton(item.question_id.ToString(),4)4
@RadioButton(item.question_id.ToString(),5)5
注释:@Html.TextArea(item.question_id.ToString()+“_comment”,new{@class=“form control”,@cols=200,@rows=5})
}
}
我假设您需要的是客户端验证:
$('.radio-inline').change(function() {
var commentQuestionName = $(this).attr('name') + '_comment';
if ($(this).val() < 3) {
$('input[name="' + commentQuestioName + '"]').show();
}
else {
$('input[name="' + commentQuestioName + '"]').hidden();
}
});
$('.radio inline').change(函数(){
var commentQuestionName=$(this.attr('name')+'u comment';
if($(this.val()<3){
$('input[name=“”+CommentQuestionName+“]”)。show();
}
否则{
$('input[name=“”+CommentQuestionName+“]”)。hidden();
}
});
试试下面的代码片段。希望这对你有帮助
$('.radio-inline > input').change(function() {
var val = $(this).val();
var comment = $(this).closest('.form-group').find('textarea.form-control');
if (val < 3) {
comment.attr('required', 'required');
}
else {
comment.removeAttr('required');
}
});
$('.radio inline>input')。更改(函数(){
var val=$(this.val();
var comment=$(this).closest('.form group').find('textarea.form control');
if(val<3){
comment.attr('required','required');
}
否则{
注释。removeAttr(“必需”);
}
});
已经接受了一个答案,但是,当您使用MVC时,我建议您使用自定义的属性。这里有一个类似于您需要的:考虑使用<代码> [Reavdif] < /C>或类似的验证属性,从而得到客户端和服务器端验证。