如何获取 <script> 标签内函数的 asp:textbox 内容?
How to get asp:textbox contents for a function that's inside a <script> tag?
这是我在 ASP.NET:
中完成的简单登录页面的完整代码
<p></p>
User:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<p></p>
Pass:
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
<p></p>
<asp:Label ID="Label1" runat="server" Text="" />
<div id="wrapper">
<button id="buttona" type="button">Sweet Alert!</button>
</div>
<script>
$('#buttona').click(function () {
if (('#TextBox1').valueOf == "user" && ('#TextBox2').valueOf == "pass") {
swal("Yay!", "You logged in succesfully", "success");
}
else {
swal("Oh noez", "That's incorrect", "error");
}
});
</script>
目标是在登录成功时显示肯定消息,在登录失败时显示错误消息(使用 SweetAlert)。
我的问题是它总是显示 "Incorrect" 消息,我认为问题在于它没有正确读取 TextBox 内容(语法对我来说看起来不太正确,但我不是确定它应该如何)。我也试过用 .text
替换 .valueOf
但没有用。
如何获取每个 asp:TextBox
中输入的内容(文本)?
您可以通过
获取 TextBox1 和 TextBox2 的值
var textbox1 = document.getElementById('<%= TextBox1.ClientID %>').value;
var textbox2 = document.getElementById('<%= TextBox2.ClientID %>').value;
这是我在 ASP.NET:
中完成的简单登录页面的完整代码<p></p>
User:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<p></p>
Pass:
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
<p></p>
<asp:Label ID="Label1" runat="server" Text="" />
<div id="wrapper">
<button id="buttona" type="button">Sweet Alert!</button>
</div>
<script>
$('#buttona').click(function () {
if (('#TextBox1').valueOf == "user" && ('#TextBox2').valueOf == "pass") {
swal("Yay!", "You logged in succesfully", "success");
}
else {
swal("Oh noez", "That's incorrect", "error");
}
});
</script>
目标是在登录成功时显示肯定消息,在登录失败时显示错误消息(使用 SweetAlert)。
我的问题是它总是显示 "Incorrect" 消息,我认为问题在于它没有正确读取 TextBox 内容(语法对我来说看起来不太正确,但我不是确定它应该如何)。我也试过用 .text
替换 .valueOf
但没有用。
如何获取每个 asp:TextBox
中输入的内容(文本)?
您可以通过
获取 TextBox1 和 TextBox2 的值var textbox1 = document.getElementById('<%= TextBox1.ClientID %>').value;
var textbox2 = document.getElementById('<%= TextBox2.ClientID %>').value;