Javascript在母版页中不工作
我有一个使用母版页的登录页面,我通过javascript应用了验证,但它不起作用Javascript在母版页中不工作,javascript,asp.net,.net,master-pages,web-deployment,Javascript,Asp.net,.net,Master Pages,Web Deployment,我有一个使用母版页的登录页面,我通过javascript应用了验证,但它不起作用 <script type="text/javascript"> function checkField(form) { var id = document.getElementById('<% =txtLoginId.ClientID %>').value; var pwd = document.getElementById('<% =
<script type="text/javascript">
function checkField(form)
{
var id = document.getElementById('<% =txtLoginId.ClientID %>').value;
var pwd = document.getElementById('<% =txtPassword.ClientID %>').value;
alert("testing" + id);
if ((id == '' && pwd != '' )|| (id == null && pwd != null)) {
lblUserId.visible = true;
form.txtLoginId.focus();
return false;
}
if ((pwd == '' && id != '') || (pwd == null && id != null)) {
lblPwd.visible = true;;
form.txtPassword.focus();
return false;
}
if ((id == '' && pwd == '') || (id == null && pwd == null)) {
lblBoth.visible = true;
form.txtLoginId.focus();
return false;
}
}
</script>
函数检查字段(表单)
{
var id=document.getElementById(“”).value;
var pwd=document.getElementById(“”).value;
警报(“测试”+id);
if((id=''&&pwd!='')| |(id==null&&pwd!=null)){
lblUserId.visible=true;
form.txtLoginId.focus();
返回false;
}
如果((pwd=''&&id!='')| |(pwd==null&&id!=null)){
lblPwd.visible=true;;
form.txtPassword.focus();
返回false;
}
if((id=''&&pwd='')| |(id==null&&pwd==null)){
lblBoth.visible=真;
form.txtLoginId.focus();
返回false;
}
}
如果我在用户名中给出了一些值,它会在该警报中显示出来,但如果我没有给出任何值,那么if条件应该起作用,但它不起作用,警报只显示“测试”。
这是我的html代码
<form id="form" method="post" action="Login.aspx">
<div style="left:37%;position:absolute;top:55%;background-color:red">
<table style="left:0%; position:absolute;top:0%; width: 265px">
<asp:Label ID="lblUserId" runat="server" ForeColor="Red" Visible="false" Text="* Username is Required!"></asp:Label>
<asp:Label ID="lblPwd" runat="server" ForeColor="Red" Visible="false" Text="* Passowrd is Required!"></asp:Label>
<asp:Label ID="lblBoth" runat="server" ForeColor="Red" Visible="false" Text="* Username and Password are Required!"></asp:Label>
</table>
</div>
<div style="left:37%;position:absolute;top:60%;background-color:red">
<table style="left:0%; position:absolute;top:0%; width: 265px;border:solid;background-color:darkorange">
<tr align="center">
<td align="center">
<asp:Label ID="lblHead" runat="server" Font-Bold="true" Text="Mobile Customer Order Tracking"></asp:Label>
</td>
</tr>
</table>
</div>
<div style="left:37%;position:absolute;top:65%">
<table style="border:solid;">
<tr>
<td>
<asp:Label ID="lblLoginId" runat="server" Text="Login ID"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtLoginId" ClientIDMode="Static" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtPassword" TextMode="Password" ClientIDMode="Static" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID ="lblRemember" runat="server" Text="Remember My ID"></asp:Label>
<asp:CheckBox ID ="chkRemember" runat="server" />
</td>
<td align="right">
<asp:Button ID="btnLogin" runat="server" text="Login" OnClientClick="return checkField(this);"/>
</td>
</tr>
</table>
</div>
</form>
是否检查了javascript异常 您正在通过服务器端ID引用asp元素,前端找不到该ID:
if ((id == '' && pwd != '' )|| (id == null && pwd != null)) {
lblUserId.visible = true; //here
form.txtLoginId.focus();
return false;
}
if ((pwd == '' && id != '') || (pwd == null && id != null)) {
lblPwd.visible = true; //here
form.txtPassword.focus();
return false;
}
if ((id == '' && pwd == '') || (id == null && pwd == null)) {
lblBoth.visible = true; //here
form.txtLoginId.focus();
return false;
}
解决方案:
这可以通过为这些用户控件设置
clientdmode=“static”
来解决,也可以像您在其他地方所做的那样在javascript中使用
。您应该使用必需的字段验证器,它省时、高效、客户端,并且不需要太多努力。删除|
部分条件。当您使用松散相等时,null
也会用==''
进行检查。它也不是这样工作的..您正在向函数传递一个按钮,而不是表单,因此form.txtLoginId
在checkField()
中是未定义的。这会破坏代码,并且永远不会返回false
。您还没有打开控制台查看错误消息吗?我打开控制台,它显示“ReferenceError:lblBoth未定义”Uhh。。对不起,我忘了那些。您必须使用document.getElementById
,就像您在函数开头使用它一样。“TypeError:document.getElementById(…)为null”此错误出现。