Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.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
C# 使用jQuery在asp.net mvc razor中设置表单的必需项_C#_Jquery_Asp.net_Asp.net Mvc_Razor - Fatal编程技术网

C# 使用jQuery在asp.net mvc razor中设置表单的必需项

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">

我有这个问题:我创建了反馈表单(表单中是问题,然后是结果(1到5的单选按钮)和注释),这个反馈有很多问题,这些问题是从数据库表问题中加载的。我想动态设置,当用户给出的结果少于3个问题时,对这个问题的评论成为必需的。另一方面,当用户给出更好的结果(如mark 3)时,不需要对此问题进行评论

这是我的反馈表:

@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>或类似的验证属性,从而得到客户端和服务器端验证。