gpt4 book ai didi

Javascript 无法在母版页中工作

转载 作者:行者123 更新时间:2023-12-03 11:36:42 24 4
gpt4 key购买 nike

我有一个使用母版页的登录页面,我通过 JavaScript 应用了验证,但它不起作用。

<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>

如果我在用户名中给出一些值,它会在该警报中显示我,但如果我没有给出任何值,那么 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 元素,前端不会找到它:

    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;
}

解决方案:

这可以通过设置 ClientIDMode = "static" 来解决对于这些用户控件,或使用 <%= lblUserId.ClientID %>就像您在其他地方所做的那样,在 javascript 中。

关于Javascript 无法在母版页中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26467603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com