如何验证特定的Div

时间:2014-07-09 09:28:13

标签: c# jquery asp.net-mvc

我正在尝试验证我在谷歌上看到的特定div,我找不到任何关于我进行验证的方式的示例。我有2个div我希望能够单击一个按钮并验证第一个div然后我希望该div隐藏并验证第二个div。我已经编写了一些隐藏第一个div的jquery代码,但我只想在验证特定div后才能执行此操作。

MARKUP

<div id="contentone">

<table>
<tr>
<td>game console</td>
<td><%=Html.TextBox("console", ViewData["console"] ?? "") %></td>
</tr>
<tr>
<td>manafacturer</td>
<td><%=Html.TextBox("manaf", ViewData["manaf"] ?? "") %></td>
</tr>
</table>

</div>

<div id="contenttwo">

<table>
<tr>
<td>description</td>
<td><%=Html.TextBox("desc", ViewData["desc"] ?? "") %></td>
</tr>
<tr>
<td>Available games</td>
<td><%=Html.TextBox("games", ViewData["games"] ?? "") %></td>
</tr>
</table>

</div>


 <button id="hide" type="button">Test1</button>


 <script>

         $(document).ready(function(){
             $("#hide").click(function(){
                 $("#contentone").hide();
             });
         }); 

 </script>

CONTROLLER

[AcceptVerbs("POST")]
    public ActionResult ConsoleQues(string console, string manaf, string desc, string games)
    {
        ViewData["console"] = console;
        ViewData["manaf"] = manaf;
    ViewData["desc"] = desc;
        ViewData["games"] = games;




        if (string.IsNullOrEmpty(console))
            ModelState.AddModelError("console", "Please enter console name ");
        if (string.IsNullOrEmpty(manaf))
            ModelState.AddModelError("manaf", "please enter manafactuer name");
      if (string.IsNullOrEmpty(desc))
            ModelState.AddModelError("desc", "Please enter description name ");
        if (string.IsNullOrEmpty(games))
            ModelState.AddModelError("games", "please enter game name");

1 个答案:

答案 0 :(得分:1)

创建验证两个div的函数:

function IsValid(divid) {
    var $div = $('#' + divid);
    var result = true;
    $.each($div.find("input[type='text']"), function (i, input) {
        if ($(input).val().length == 0 || $.trim($(input).val()) == '') {
            result = false;
            return;
        }
    });

    return result;
}

检查有效性并隐藏contentone div

if (IsValid('contentone')) {
    $('#contentone').hide();
}