2011-08-09 94 views
2

不知道我在做什么错在这里,但由于某些原因,我的jQuery验证代码是搞乱我的网站......jquery验证不工作?

只是一个小的背景下,我实现一个asp.net MVC2网站,将显示一个来自控制器的XML数据。我正在考虑从控制器获取JSON数据,但我需要做些工作。无论如何,所以我的所有行和列都是即时生成的。现在我有一个窗口,它是模态弹出窗口(实际上这只是一个ascx文件...)我使用从控制器获得的xml生成类似的表单。将Jquery验证元素放在那里可能会有点令人痛苦。这是我试图做的...

foreach (string row in paths) 
    { 
     Response.Write(String.Format("<tr><td>{0}</td><td>", row)); 
     System.Xml.XmlAttributeCollection nodes = TemplateToEdit.SelectSingleNode(string.Format("./elements/element[@name='{0}']", row)).Attributes; 
     System.Xml.XmlNode validationNode = TemplateToEdit.SelectSingleNode(string.Format("./elements/element/validation", row)); 
     if ((nodes["readOnly"].Value == "false") || (nodes["visable"].Value == "true")) 
     { 
      if (validationNode != null) 
      { 
       System.Xml.XmlNode rule = validationNode.SelectSingleNode("./rules/rule"); 
       System.Xml.XmlAttributeCollection ruleAttributes = rule.Attributes; 
       //ruleAttributes["test"] 
      } 
      //initialVal = xTemplate.SelectSingleNode(string.Format("//dataTemplateSpecification/templates/template/elements/element[@value='{0}']", paths[i])).Value; 
      switch (nodes["dataType"].Value) 
      { 
       case "String": 
        if (row == "MRNType") 
        { 
        %><label for="<%row %>"><%=Html.DropDownList(string.Format("{0}", row), (List<SelectListItem>)TempData["MRNLIST"])%></label><% 
        } 
        else{ 
        %><label for="<%row %>"> 
        <%=Html.TextBox(string.Format("{0}", paths[i]), (nodes["value"].Value == null ? "" : nodes["value"].Value), new { id = nodes["id"].Value, @class = "{validate:true, "+paths[i]+":true, messages:{required:'Please enter a value'}}" })%></label><!--string input -->       
        <% Response.Write("</td>");       
         } 
        %><% 
         break; 
       case "int": 
        Response.Write("");      
        %><label for="<%row %>"><%=Html.TextBox(string.Format("{0}", paths[i]), (nodes["value"].Value == null ? "" : nodes["value"].Value), new { id = nodes["id"].Value, @class = "{validate:true, " + paths[i] + ":true, messages:{required:'Please enter a value'}}" })%></label> <!--string input --><% 
        Response.Write("</td>"); 
        break; 
       case "KeyValuePair": 
        Response.Write(""); 
        %><%=Html.RadioButton(string.Format("{0}", paths[i]), "1", new { id = nodes["id"].Value })%>Yes<% 
        %><%=Html.RadioButton(string.Format("{0}", paths[i]), "0", new { id = nodes["id"].Value })%>No<% 
        Response.Write("</td>"); 
        break; 
       case "Date": 
        %><label for="<%row %>"><%=Html.TextBox(string.Format("{0}", paths[i]), (row.Contains("Date") ? nodes["value"].Value : row), new { @class = "datepicker" + dateCount, Class = "{validate:true, " + paths[i] + ":true, messages:{required:'Please enter a value'}}" })%></label><!--supposed to be date input --><% 
        Response.Write("</td>"); 
        dateCount++; 
        break; 
      } 
      Response.Write("</tr>"); 
     } 
     i++; 
    } 

正如你所看到的,很混乱。但让我为你分解它,我只是遍历每个XML元素,并为它写一个相应的表单元素,无论它需要一个简单的文本框还是单选按钮,主要由数据类型决定... 让我们看看一些简单的输入验证码我在那里...

@class = "{validate:true, " + paths[i] + ":true, messages:{required:'Please enter a value'}}" 

正如你所看到的,我创建具有它内部的一些验证代码的类?这将是我之前的一个堆栈溢出问题的答案,当我完全完成这个站点时,我一定会填写它,但我离题了。
下面是应该是微不足道的我简单而优雅的代码...

 $(document).ready(function() { 
    $(".temp1").validate(); 
    }); 

这应该工作的权利?那么我得到的印象是它不工作,因为我的Modal窗口不会弹出这个jQuery代码运行。这是我有什么不对的第一条线索。如果有人能帮助我,我将不胜感激。

回答

0

只是看着它很快,但如果你想运行验证码尝试使用:

$(document).ready(function() { 
    $(".temp1").valid(); 
});