简体   繁体   English

如果一行中的其他任何文本框都已填写,则是否需要连续一行文本框?

[英]Make text boxes in a row required if any other text box of a row is filled?

I am posting my FormCollection to my HttpPost Action in Asp.Net MVC . 我将我的FormCollection发布到Asp.Net MVC HttpPost Action中。 Everything is working fine but the problem is validation. 一切正常,但问题是验证。 I have 3 Rows each with 3 textboxes. 我有3行,每个行有3个文本框。 I want to make sure through JavaScript or C# that if any textbox of a row is filled then others are required. 我想通过JavaScriptC#确保如果一行的任何textbox都已填充,则需要其他textbox I cannot make it static since rows and textboxes are very dynamic. 由于行和文本框是非常动态的,因此我不能将其设置为静态。 We could have five rows, six or many. 我们可以有五行,六行或很多行。 Following is my code: 以下是我的代码:

@{
     int count = 0;
     foreach (var record in Model)
     {
       <tr>
         <td> 
            @Html.Hidden(count.ToString() + "Record", record.Id.ToString())
         </td>
         <td>
                <input type="text" name="@count.ToString()Code" />         
         </td>
         <td>
                <input type="text" name="@count.ToString()Name" />     
         </td>           
       </tr>
       count = count + 1;
     }
     @Html.Hidden("TotalRecords", count)
}

Suppose, I have three rows each with code and name. 假设我有三行,每行分别包含代码和名称。 If the code of row1 is filled, I need to make sure the name of 'row1' should be filled to. 如果填充了row1的代码,则需要确保将'row1'的名称填充为。 How can I do this? 我怎样才能做到这一点?

You can handle it by adding class to all your textboxes. 您可以通过将class添加到所有文本框来处理它。 Verify that if textbox with that class has no value then display message as shown in below code 验证该类的文本框是否没有值,然后显示消息,如以下代码所示

 $("#submit").click(function(){ var Flag=0; debugger $('.textBox').each(function(i) { if($(this).val()=="") { if(Flag==0) { Flag=1+i; } } }); if(Flag!=0) { alert("Please fill textbox : "+Flag) } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" class="textBox" > <br /> <input type="text" class="textBox" > <br /> <input type="text" class="textBox" > <br /> <input type="button" id="submit" value="submit" > 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM