嘿,所以我有两个文本框和一个按钮。我只希望在“playerName”文本框包含某些内容并且“upperLimit”文本框包含整数 > 0 时启用该按钮。我希望该按钮开始禁用,然后随着用户在文本框中键入内容而动态更改,不断检查中的内容文本框来查看按钮是否应该被激活。这是我尝试过的:
JavaScript:
var playerNameValid = false;
var upperLimitValid = false;
function validatePlayerName()
{
if (document.getElementById("initialPlayerNameChoice").Value == "")
{
playerNameValid = false;
document.getElementById("startGameButton").disabled = true;
}
else
{
playerNameValid = true;
if (upperLimitValid == true)
{
document.getElementById("startGameButton").disabled = false;
}
}
}
function validateUpperLimit()
{
if (isNaN(document.getElementById("initialUpperLimitChoice").valuetextContent))
{
upperLimitValid = false;
document.getElementById("startGameButton").disabled = true;
}
else
{
upperLimitValid = true;
if (playerNameValid == true)
{
document.getElementById("startGameButton").disabled = false;
}
}
}
标记:
<asp:Label ID="enterNameLabel" runat="server" Text="Enter your name: "></asp:Label>
<asp:TextBox ID="initialPlayerNameChoice" runat="server"></asp:TextBox>
<br /><br/>
<asp:Label ID="enterUpperLimitLabel" runat="server" Text="Enter upper limit: "></asp:Label>
<asp:TextBox ID="initialUpperLimitChoice" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="startGameButton" enabled="false" runat="server" Text="Start Game" />
代码背后:
initialPlayerNameChoice.Attributes.Add("onkeyup", "javascript:validatePlayerName()");
initialUpperLimitChoice.Attributes.Add("onkeyup", "javascript:validateUpperLimit()");