我在母版页中有一个jquery表单验证,它工作正常,我从这篇文章开始工作:http://www.dotnetcurry.com/ShowArticle.aspx?ID=310
我的问题是:如果我将.js放到外部并添加对我的页面的引用然后它不起作用...它说预期的对象
这是我的表现:
在我的内容页面中(我正在使用母版页,asp.net)
在我的内容页面中添加:
<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 as text for IE
label.html(" ").addClass("checked");
}
});
} ;
答案 0 :(得分:1)
我怀疑它是因为服务器没有处理您的服务器标签(<%=TextBox1.UniqueID%>
)。默认情况下,IIS不处理.js文件。
答案 1 :(得分:1)
答案 2 :(得分:1)
为什么要尝试将javascript放入外部文件?该脚本特定于页面上的控件,因此请将其保留在那里。否则,您需要额外的文件下载,从而为页面添加延迟。