javascript - 如何在mvc4中为以下代码编写javascript或jquery验证?

标签 javascript jquery validation asp.net-mvc-4

如果用户选中任何复选框,我想将 TextBox 验证为必填字段。

如何验证上述要求。有人可以帮忙吗?

下面是查看代码片段。

<div>
         <div id="">
            <input type="checkbox"  id="partsChk" name="partsChk"/><label      
            class="enrollment_side">
                Parts (including IRFs)
            </label>
        </div>


          <div id="">
            @Html.TextBox("PartsTitle","", new { @class = "enrollment_tbox", placeholder 
              = "Parts Contact Title (i.e. Parts Manager)" })


             <br />
            @Html.TextBox("PartsFirstName", "", new { placeholder = "First Name" })
            @Html.TextBox("PartsLastName","", new { placeholder = "Last Name" })
            @Html.TextBox("PartsEmpId","",  new { placeholder = "BMW Employee ID" })


            <br />

            </div>

             </div>


            <div>
         <div id="">
           <input type="checkbox" id="salesChk" name="salesChk" /><label  
        class="enrollment_sidehdr">Sales – Coming in Q3 2014
            </label>
        </div>


             <div id=""> 
        @Html.TextBox("SalesTitle","", new { @class = "enrollment_tbox", placeholder =  
        "Sales Contact Title (i.e. Sales Manager)" })

            <br />

          @Html.TextBox("SalesFirstName", "", new { placeholder = "First Name" })
            @Html.TextBox("SalesLastName","", new { placeholder = "Last Name" })
            @Html.TextBox("SalesEmpId","",  new { placeholder = "BMW Employee ID" })

        </div>

       </div>


     <div>
      <div id="">
       <input type="checkbox" id="serviceChk" name="serviceChk" /><label  
       class="enrollment_sidehdr">Service – Coming in Q3 2014
            </label>
        </div>


        <div id="">
           @Html.TextBox("ServiceTitle","", new { @class = "enrollment_tbox", 
        placeholder = "Service Contact Title (i.e. Service Manager)" })
            <br />

           @Html.TextBox("ServiceFirstName", "", new { placeholder = "First Name" })
            @Html.TextBox("ServiceLastName","", new { placeholder = "Last Name" })
            @Html.TextBox("ServiceEmpId","",  new { placeholder = "BMW Employee ID" })


            <br />

        </div>

         </div>

最佳答案

请引用此示例代码。我已经编写了 HTML 和 jQuery 代码,请将其集成到您的应用程序中

HTML

Group 1
<input type="checkbox"  id="1" class="cb"/>
<input type="text" class="txt1">
<input type="text" class="txt1">
    <br/>
    <br/>
Group 2
<input type="checkbox"  id="2" class="cb"/>
<input type="text" class="txt2">
<input type="text" class="txt2">
    <br/>
    <br/>
Group 3
<input type="checkbox"  id="3" class="cb"/>
<input type="text" class="txt3">
<input type="text" class="txt3">
    <br/><br/>
    <input type="button" id="button1" value="click me" />

jQuery

$("#button1").click(function(){
    $(".cb").each(function(){
        var flag = 0;
        if($(this).is(':checked'))
        {
            $(".txt" + $(this).attr("id")).each(function(){
                if($(this).val() == "")
                {
                    flag = 1;
                }
            });
            if(flag == 1)
            {
                alert("Fields of Group " + $(this).attr("id") + " are empty, Please fill it");
            }
        }
    });
});

<强> Live demo here

关于javascript - 如何在mvc4中为以下代码编写javascript或jquery验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22371574/

相关文章:

javascript - 将 <div>...</div> 导出为包含所有资源的 html 文档(如在线网页模板生成器)

ruby-on-rails - 我在源代码/文档中的哪里可以找到ActiveModel::Errors default error message values?

当我尝试上传 ai 或 psd 文件时,Laravel 图像验证不起作用

javascript - JQuery 只返回一个值

javascript - form.submit 触发多次,每次响应后额外触发一次

java - 在业务层返回业务验证结果的首选方法是什么

javascript - 如何在 html 中将文本字段输入从 1234567891234 转换为 1234-56-789-1234?

javascript - 如何防止多个http请求同时触发

javascript - 无法使用 public 文件夹中的 Meteor 图像

javascript - 更改 Bee3D slider 上的图像大小