2010-04-23 55 views
0

我在母版页一个jQuery表单验证,它工作正常,我得到了从这篇文章的工作:http://www.dotnetcurry.com/ShowArticle.aspx?ID=310jQuery的表单验证:验证脚本指定外部

我的问题是:如果我放在.js文件外部和引用添加到我的网页,然后它不工作...它说对象预期

这里是如何我做:

在我的内容页

(我使用母版页,asp.net)

加入我的骗局10吨页:

<script src="myform_validation.js" type="text/javascript"></script> 
<script type="text/javascript"> 

    $(document).ready(function() {  
     ValidateMe(this); 
    }); 
</script> 

下面是在外部.js文件:

function ValidateMe() { 

      $("#aspnetForm").validate({ 
       rules: 
       { 
         <%=TextBox1.UniqueID %>: 
         { 
          maxlength:1, 
          //minlength: 12, 
          required: true 
         }, 
         <%=TextBox2.UniqueID %>: 
         { 
          minlength: 12, 
          required: true 
         }, 
         <%=TextBox3.UniqueID %>: 
         { 
          minlength: 12, 
          required: true 
         }//, 
//      
       },   
       messages: 
        { 
         <%=TextBox1.UniqueID %>: 
         { 
          required: "Enter your firstname", 
          minlength: jQuery.format("Enter at least {0} characters") 
         }, 
         <%=TextBox2.UniqueID %>: 
         { 
          required: "Please enter a valid email address", 
           minlength: "Please enter a valid email address" 
         } , 
          <%=TextBox3.UniqueID %>: 
         { 
          required: "Enter your firstname", 
          minlength: jQuery.format("Enter at least {0} characters") 
         } 

        } , 


      success: function(label) { 
      // set &nbsp; as text for IE 
      label.html("&nbsp;").addClass("checked"); 
     } 

     }); 
     } ; 
+0

您是否在外部js中包含了jquery引用? – melaos 2010-04-23 14:56:35

回答

1

我怀疑它,因为你的服务器标签(<%=TextBox1.UniqueID%>)不被服务器处理。默认情况下,IIS不处理.js文件。

+0

+1。是的,这可能是,没有看到这个想法:) – ntziolis 2010-04-23 15:04:23

1
  1. 对于初学者,尝试始终使用与jQuery相同的 。 有时候你使用jQuery的有时 $,我会建议 使用$一路只是 由于其短,众所周知 (和IM懒惰;))。
  2. 虽然它没有参数,但您仍将其传递到 外部ValidateMe函数,即使是 。你确定 确定你正确提取了功能 ?
  3. 并始终确保所有js文件 被引用,然后使用其中的函数启动 。
+0

感谢您的提示,但我没有看到我在哪里使用JQuery,我也喜欢使用$ :) 关于您的#2)我正在尝试不同的方法和非他们的作品,传递参数,并且不传递参数 – 2010-04-23 15:29:36

+0

@Abu - 例如:jQuery.format(“至少输入{0}个字符”) – ntziolis 2010-04-26 12:49:24

+0

哎呀,我明白了:) – 2010-04-26 14:08:11

1

你为什么试图把javascript放到外部文件中?该脚本特定于页面上的控件,因此请将其留在那里。否则,您需要额外的文件下载来为页面添加延迟。

+0

@Daniel,我这样做的原因是因为它会在页面上显得杂乱无章,只是想保持整洁和简单。 – 2010-04-23 16:07:42