jquery - 如何在 MVC 中单击按钮时选中复选框?

标签 jquery asp.net-mvc

我想在用户填写第 1 部分的最后一项时选中复选框基本信息,在填写联系方式详细信息部分时选中复选框联系方式,依此类推每次填充或选择最后一个项目时,Next 按钮颜色也会更改为绿色。 另外,我还有一般信息部分,当用户移动到下一部分时,内容会从基本信息更改为联系方式,然后在第三部分中选择类(class) .

脚本

 $('.next').click(function () {
            var container = $(this).closest('.section');
            var isValid = true;
            $.each(container.find('input'), function () {
                $('form').validate().element($(this));
                if (!$(this).valid()) {
                    isValid = false;
                    return false;
                }

            });
            if (isValid) {
                container.next('.section').show().find('input').first().focus();
                container.hide();
            } else {
                container.find('.error').text('please complete fields');
            }           

        });
        $('.previous').click(function () {
            debugger;
            var container = $(this).closest('.section');
            container.prev('.section').show().find('input').first().focus();
            container.hide();
        });

查看

@using (Html.BeginForm("OnlineEnrolment", "Home", FormMethod.Get))
{
    @Html.AntiForgeryToken()
    <div class="container">
        <div class="form-horizontal">

            <div class="col-md-3">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <span class="panelfont"> 1 APPLY</span>
                        <div class="navheader">
                            @Html.LabelFor(m => m.IsBasicInfo, "basic info")
                            @Html.CheckBoxFor(m=>m.IsBasicInfo,new { @id = "basicInfo",@class="chkbox" })
                        </div>
                        <br />
                        <div class="navcontactdetails">
                            @Html.LabelFor(m => m.IsContactDetails, "contact details")
                            @Html.CheckBoxFor(m => m.IsBasicInfo, new { @id = "contactdetails", @class = "chkbox" })
                        </div>
                    </div>
                </div>
                <div class="panel panel-default">
                    <div class="panel-body">
                        <span class="panelfont"> 2 SELECT A COURSE</span>
                        <div class="navheader">
                            @Html.LabelFor(m => m.IsSelectCourse, "select a course")
                            @Html.CheckBoxFor(m => m.IsSelectCourse, new { @id = "selectcourse", @class = "chkbox" })
                        </div>
                    </div>
                </div>
                <div class="panel panel-default">
                    <div class="panel-body">
                        <span class="panelfont"> 3 FINISH</span>
                    </div>
                </div>
            </div>
            <div class="col-md-6">

                <div class="section">
                    <div class="panel panel-default">
                        <h4>BASIC INFO</h4>
                        <div class="panel-body">
                            <div class="form-group has-success ">
                                @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-ok"></i></span>
                                    </div>
                                    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                                </div>
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model.EmailId, htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    <div class="inner-addon right-addon">
                                        @Html.EditorFor(model => model.EmailId, new { htmlAttributes = new { @class = "form-control" } })
                                        <i class="glyphicon glyphicon-ok"></i>
                                    </div>
                                    @Html.ValidationMessageFor(model => model.EmailId, "", new { @class = "text-danger" })
                                </div>
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model.DOB, htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    <div class="inner-addon right-addon">
                                        @Html.EditorFor(model => model.DOB, new { htmlAttributes = new { @class = "form-control" } })
                                        <i class="glyphicon glyphicon-calendar"></i>
                                    </div>

                                    @Html.ValidationMessageFor(model => model.DOB, "", new { @class = "text-danger" })
                                </div>
                            </div>

                            <div class="form-group">

                                <div class="CoursesStudiedYesNo" style="padding-left:6em;">
                                    @Html.Label("Have you studied before?")
                                    <br />
                                    @Html.RadioButtonFor(m => m.StudiedYesNo, "Yes")<span id="SpacebetweenRadioandText">Yes</span><br />
                                    @Html.RadioButtonFor(m => m.StudiedYesNo, "No", new { @checked = "checked" })<span id="SpacebetweenRadioandText">No</span>
                                </div>

                            </div>
                            <div class="form-group ">

                                <div class="CoursesDropdown" style="padding-left:6em;">
                                    @Html.LabelFor(model => model.CoursesStudied, htmlAttributes: new { @class = "control-label " })
                                    <br />
                                    @Html.DropDownListFor(m => m.CoursesStudied, Model.CoursesStudiedList, "Choose the course you have studied here", new { @class = "form-control" })

                                    @Html.ValidationMessageFor(model => model.CoursesStudied, "", new { @class = "text-danger" })
                                </div>
                            </div>

                            <div class="error"></div>
                            <button type="button" class="next pull-right">NEXT</button>

                        </div>
                    </div>
                </div>
                <div class="section">
                    <div class="panel panel-default">
                        <h4>CONTACT DETAILS</h4>
                        <div class="panel-body">
                            <div class="form-group">
                                @Html.Label("Phone", htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    <div class="inner-addon right-addon">
                                        @Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
                                        <i class="glyphicon glyphicon-ok"></i>
                                    </div>

                                    @Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" })
                                </div>
                            </div>

                            <div class="form-group">
                                @Html.Label("Mobile", htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    <div class="inner-addon right-addon">
                                        @Html.EditorFor(model => model.mobileNumber, new { htmlAttributes = new { @class = "form-control" } })
                                        <i class="glyphicon glyphicon-ok"></i>
                                    </div>

                                    @Html.ValidationMessageFor(model => model.mobileNumber, "", new { @class = "text-danger" })
                                </div>
                            </div>

                            <div class="form-group">
                                @Html.Label("Address", htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    <div class="inner-addon right-addon">
                                        @Html.EditorFor(model => model.Street, new { htmlAttributes = new { @class = "form-control", placeholder = "Street" } })<br />
                                        <i class="glyphicon glyphicon-ok"></i>
                                    </div>
                                    <div class="inner-addon right-addon">
                                        @Html.EditorFor(model => model.Suburb, new { htmlAttributes = new { @class = "form-control", placeholder = "Sub" } })<br />
                                        <i class="glyphicon glyphicon-ok"></i>
                                    </div>
                                    <div class="inner-addon right-addon">
                                        @Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control", placeholder = "City" } })<br />
                                        <i class="glyphicon glyphicon-ok"></i>
                                    </div>
                                    <div class="inner-addon right-addon">
                                        @Html.EditorFor(model => model.Region, new { htmlAttributes = new { @class = "form-control", placeholder = "Region" } })<br />
                                        <i class="glyphicon glyphicon-ok"></i>
                                    </div>
                                    <div class="inner-addon right-addon">
                                        @Html.EditorFor(model => model.Country, new { htmlAttributes = new { @class = "form-control", placeholder = "Country" } })<br />
                                        <i class="glyphicon glyphicon-ok"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="error"></div>
                            <button type="button"  class="previous pull-left">PREVIOUS</button>
                            <button type="button" class="next pull-right">NEXT</button>

                        </div>
                    </div>
                </div>
                <div class="section">
                    <div class="panel panel-default">
                        <h4>SELECT A COURSE</h4>
                        <div class="panel-body">
                            <div class="form-group">
                                @Html.Label("Course", htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.DropDownListFor(m => m.CourseName, Model.CourseNameList, "Choose a course", new { @class = "form-control" })
                                </div>
                            </div>
                            <div class="error"></div>
                            <button type="button" class="previous pull-left">PREVIOUS</button>
                            <button type="button" class="next pull-right">NEXT</button>
                        </div>
                    </div>
                </div>
                <div class="section">
                    <div class="panel panel-default">
                        <h4>SUMMARY</h4>
                        <div class="panel-body">
                            <div>
                                <dl class="dl-horizontal">
                                    <dt>
                                        @Html.DisplayNameFor(model => model.Name)
                                    </dt>

                                    <dd id="display-name">
                                        @Html.DisplayFor(model => model.Name)
                                    </dd>

                                    <dt>
                                        @Html.DisplayNameFor(model => model.EmailId)
                                    </dt>

                                    <dd id="display-emailid">
                                        @Html.DisplayFor(model => model.EmailId)
                                    </dd>

                                    <dt>
                                        @Html.DisplayNameFor(model => model.DOB)
                                    </dt>

                                    <dd id="display-dob">
                                        @Html.DisplayFor(model => model.DOB)
                                    </dd>

                                    <dt>
                                        @Html.DisplayName("Study History")
                                    </dt>

                                    <dd id="display-coursesstudied">
                                        @Html.DisplayFor(model => model.CoursesStudied)
                                    </dd>

                                    <dt>
                                        @Html.DisplayNameFor(model => model.PhoneNumber)
                                    </dt>

                                    <dd id="display-phone">
                                        @Html.DisplayFor(model => model.PhoneNumber)
                                    </dd>

                                    <dt>
                                        @Html.DisplayNameFor(model => model.mobileNumber)
                                    </dt>

                                    <dd id="display-mobile">
                                        @Html.DisplayFor(model => model.mobileNumber)
                                    </dd>

                                    <dt>
                                        @Html.DisplayName("Address")
                                    </dt>

                                    <dd id="display-street">
                                        @Html.DisplayFor(model => model.Street)
                                    </dd>

                                    <dd id="display-suburb">
                                        @Html.DisplayFor(model => model.Suburb)
                                    </dd>

                                    <dd id="display-city">
                                        @Html.DisplayFor(model => model.City)
                                    </dd>

                                    <dd id="display-region">
                                        @Html.DisplayFor(model => model.Region)
                                    </dd>

                                    <dd id="display-country">
                                        @Html.DisplayFor(model => model.Country)
                                    </dd>

                                    <dt>
                                        @Html.DisplayName("Selected Course")
                                    </dt>

                                    <dd id="display-coursename">
                                        @Html.DisplayFor(model => model.CourseName)
                                    </dd>

                                </dl>
                            </div>
                            <div class="error"></div>
                            <button type="button" class="previous pull-left">PREVIOUS</button>
                            <button type="submit"  class="submit pull-right">SUBMIT</button>

                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <span class="panelfont"> GENERAL INFORMATION</span>
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div id="basic">
                            <span class="panelfont"> Step 1: Basic Info</span>
                            <p>This is the basic information of this online portal</p>
                        </div>
                        <div id="contact">
                            <span class="panelfont"> Step 1: Contact details</span>
                            <p>This is the contact details of online portal</p>
                        </div>
                        <div id="contact">
                            <span class="panelfont"> Step 2: Select a Course</span>
                            <p>This is the contact details of online portal</p>
                        </div>

                    </div>
                </div>
            </div>

        </div>
    </div>

}

最佳答案

您可以创建一个新函数来检查复选框,以便每次新部分form-control模糊时都可以调用它。因此,当您在那里编辑任何输入时,它会检查验证是否正常,如果正常,则会检查复选框。您需要一个与其复选框具有相同 id 的数据名称属性。我写在 codepen看起来效果很好。

    function checkit(elm) {
     var container=$(".container").find("[data-name='" + elm + "']"); 
     var isValid = true;
     $.each( container.find('input'), function () {
     $('form').validate().element($(this));
      if (!$(this).valid()) {
          isValid = false;
           return false;
       }   })
       if (isValid) { return true; } else { return false}};


       $('.next').click(function () {
        var container = $(this).closest('.section');
        var isValid = true;
        $.each(container.find('input'), function () {
            $('form').validate().element($(this));
            if (!$(this).valid()) {
                isValid = false;
                return false;   }
             });
          if (isValid) {
            container.next('.section').show().find('input').first().focus();
            container.hide();
          } else {
            container.find('.error').text('please complete fields'); }           
          });


  $('.form-control').blur(function () {
  var name = $(this).closest('.section').attr("data-name");
    var elm = $(this).closest('.section');
    if ( checkit(name)) {
      $('#'+name).prop('checked', true);

    } else {
      $('#'+name).prop('checked', false);
    } })  ;       

关于jquery - 如何在 MVC 中单击按钮时选中复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51705496/

相关文章:

javascript - 如何打开 &lt;input type ='file' > 中的特定文件夹

javascript - 获取图像可见区域状态以将 Canvas 中的图像设置在先前拖动的位置

html - MVC4 项目图像不显示

c# - 在单元测试期间呈现 View - ControllerContext.DisplayMode

javascript - 如何在jquery中选择更改时获取所选选项的数据值

javascript - JQuery Datatables 将复选框设置为选中

javascript - jquery 如果长度大于 x,则在字符之间添加破折号

jquery - 使用 Jquery 获取标签名称和第一个子数据

c# - Signalr 中心和 Twitter 流媒体

.net - ASP.NET MVC-使用Controller相应地设置主视图