首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

MVC2.0 javaScript不能用么?该怎么处理

2012-03-20 
MVC2.0 javaScript不能用么?其他字段都使用了Model层的客户端验证,为什么点击submit以后调用了js的validat

MVC2.0 javaScript不能用么?
其他字段都使用了Model层的客户端验证,为什么点击submit以后调用了js的validate()函数,页面却一点反应都没有呢?其他的错误信息却都可以显示!

HTML code
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SourceIndex.Models.User>" %><asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">    Register</asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"><script language="javascript" type="text/javascript">    function validate() {        var str1=document.getElementById("Password").innerText;        var str2=document.getElementById("ConfirmPassword").innerText;            if (str1 != str2 ) {                document.GetElementById("ConfirmPassword").style.backgroundColor = 'red';                err.innerHTML = "两次输入的密码不一致,请重新输入";            }        }</script>    <h2>Register</h2>    <%Html.EnableClientValidation(); %>    <% using (Html.BeginForm()) {%>        <%= Html.ValidationSummary(true) %>        <fieldset>            <legend>Fields</legend>                        <div class="editor-label">                <%= Html.Label("邮箱   *") %>            </div>            <div class="editor-field">                <%= Html.TextBoxFor(model => model.Email) %>                <%= Html.ValidationMessageFor(model => model.Email) %>            </div>            <div class="editor-label">                <%= Html.Label("密码   *")%>            </div>            <div class="editor-field">                <%= Html.TextBoxFor(model => model.Password) %>                <%= Html.ValidationMessageFor(model => model.Password) %>            </div>                        <div class="editor-label">                <%= Html.Label("确认密码   *")%>            </div>            <div class="editor-field">                <%= Html.TextBox("ConfirmPassword")%><label id="err"></label>            </div>                        <div class="editor-label">                <%= Html.Label("中文名   *")%>            </div>            <div class="editor-field">                <%= Html.TextBoxFor(model => model.CnName) %>                <%= Html.ValidationMessageFor(model => model.CnName) %>            </div>                        <div class="editor-label">                <%= Html.Label("英文名") %>            </div>            <div class="editor-field">                <%= Html.TextBoxFor(model => model.EnName) %>                <%= Html.ValidationMessageFor(model => model.EnName) %>            </div>            <div class="editor-label">                <%= Html.Label("地址") %>            </div>            <div class="editor-field">                <%= Html.TextBoxFor(model => model.Address) %>                <%= Html.ValidationMessageFor(model => model.Address) %>            </div>                        <div class="editor-label">                <%= Html.Label("传真") %>            </div>            <div class="editor-field">                <%= Html.TextBoxFor(model => model.Fax) %>                <%= Html.ValidationMessageFor(model => model.Fax) %>            </div>                        <div class="editor-label">                <%= Html.Label("固定电话") %>            </div>            <div class="editor-field">                <%= Html.TextBoxFor(model => model.Phone) %>                <%= Html.ValidationMessageFor(model => model.Phone) %>            </div>                        <div class="editor-label">                <%= Html.Label("手机号") %>            </div>            <div class="editor-field">                <%= Html.TextBoxFor(model => model.Tel) %>                <%= Html.ValidationMessageFor(model => model.Tel) %>            </div>                        <p>                <input id="submit" onclick="validate()" type="submit" value="提交" />            </p>        </fieldset>    <% } %>    <div>        <%= Html.ActionLink("Back to List", "Index") %>    </div></asp:Content> 



[解决办法]
<input id="submit" onclick="return validate()" type="submit" value="提交" />
将 onclick 事件改为 return validate(),另外,在 validate() 中,如果验证通过返回 true,验证不通过则返回 false
[解决办法]
if (str1 != str2 ) {//!=改用!==较好,不信用一下JSLint测试一下
document.GetElementById("ConfirmPassword").style.backgroundColor = 'red';
err.innerHTML = "两次输入的密码不一致,请重新输入";//这个err是何物?全局变量?这样使用不好
return false;
}


<input id="submit" onclick="return validate();" type="submit" value="提交" />

热点排行