将文本框值传递给javascript函数

时间:2013-12-12 08:10:58

标签: javascript jquery asp.net

我的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获得“&lt;%#UsernameTextBox.ClientID%&gt;”而不是UsernameTextBox的客户端ID。我想将UsernameTextBox和PasswordTextBox值传递给LoginPoup。

请建议。

3 个答案:

答案 0 :(得分:0)

你能试试这个,document.getElementById()

function LoginPopup(username,password) {    
    alert(username);
    alert(password);
    username = document.getElementById(username).value;
    password = document.getElementById(password).value;
}

答案 1 :(得分:0)

更正了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();
    // ...
}

答案 2 :(得分:0)

如果您使用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;
}
相关问题