将文本框值传递给javascript函数
Passing textbox value to javascript function
我的asp.net页面中有一个Js函数,如下所示:
function LoginPopup(username,password) {
alert(username);
alert(password);
username = username.val();
password = password.val();
}
我是这样使用的:
<asp:Button ID="btnLogin" runat="server" UseSubmitBehavior="false" OnClientClick="LoginPopup('<%# UsernameTextBox.ClientID %>', '<%# PasswordTextBox.ClientID %>'); return false;"
CssClass="login-button" Text="<%$ Resources: HRGELoggedOutMaster, Login %>">
</asp:Button>
但我看到LoginPopup得到的是"<%#UsernameTextBox.ClientID%>",而不是UsernameTextBox的客户端Id。我想将UsernameTextBox和PasswordTextBox值传递给LoginPoup。
请提出建议。
你能试试这个吗,document.getElementById()
function LoginPopup(username,password) {
alert(username);
alert(password);
username = document.getElementById(username).value;
password = document.getElementById(password).value;
}
更正了ClientID
语法:
<asp:Button ID="btnLogin" runat="server" UseSubmitBehavior="false" OnClientClick="LoginPopup('<%=UsernameTextBox.ClientID %>', '<%=PasswordTextBox.ClientID %>'); return false;"
CssClass="login-button" Text="<%$ Resources: HRGELoggedOutMaster, Login %>">
</asp:Button>
如果这不起作用,你可以尝试这种方式,你无论如何都没有使用服务器端的按钮:
<input type="button" onclick="LoginPopup('<%=UsernameTextBox.ClientID %>', <%=PasswordTextBox.ClientID %>')" />
在js中(我假设您使用jQuery):
function LoginPopup(username,password) {
username = $('#' + username).val();
password = $('#' + password).val();
// ...
}
如果您使用Gridview,请尝试以下示例方式
<ItemTemplate>
<asp:LinkButton ID="lnkgettext" runat="server" OnClientClick='<%# String.Format("return callme({0})", (((GridViewRow)Container).FindControl("txtName") as TextBox).ClientID) %>'
Text="Gettextboxvalue"></asp:LinkButton>
</ItemTemplate>
要测试的javascript函数是-
function callme(cntrl) {
alert(cntrl.id);
alert(cntrl.value);
return false;
}
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 正在全局范围中查找JavaScript函数
- 如何将输入(type=text)从html表单传递到javascript函数
- 如何调用这个匿名 JavaScript 函数
- 通过Ajax将JavaScript函数传递给PHP文件
- 在javascript函数中设置全局变量
- 如何在执行此特定onclick事件时执行JavaScript函数
- 使用javascript函数在页面初始化后加载jquery
- javascript函数同步
- 如何将一个JavaScript函数回调为多个函数
- 在javascript函数中使用php变量
- 代码背后调用一个JavaScript函数的按钮点击-C#
- 将JavaScript函数与HTML分离
- 组合两个javascript函数
- 在 Java 中的 JavaScript 函数中插入 Wicket 值
- javascript函数内部的代码用逗号而不是分号分隔
- href属性内的javascript函数
- 使用html表单中的参数调用JavaScript函数
- 分析Javascript函数中的多个对象
- javascript函数的:和=之间的区别