javascript 如何在按钮单击事件上将文本框值传递给 java 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6214649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:54:37  来源:igfitidea点击:

How to pass textbox value to java script on button click event

javascriptasp.net

提问by Thaadikkaaran

Possible Duplicate:
How to pass text in a textbox to javascript function?

可能的重复:
如何将文本框中的文本传递给 javascript 函数?

I need to pass the text box value as parameter of javascript method which will be fired on button click.

我需要将文本框值作为 javascript 方法的参数传递,该方法将在按钮单击时触发。

<asp:TextBox ID="rowID" runat="server"> </asp:TextBox>
        <asp:Button runat="server" ID="Click" OnClientClick="PassValue(Textboxvaluehere)"/>

PassValue is JavaScript method.

PassValue 是 JavaScript 方法。

Thanks in Advance.

提前致谢。

采纳答案by Bala R

<asp:Button runat="server" ID="Click" OnClientClick="GetTextBoxValue('<%= rowID.ClientID %>'.value)"/>

回答by Yuriy Rozhovetskiy

<asp:TextBox ID="rowID" runat="server"> </asp:TextBox>
<asp:Button runat="server" ID="Click" OnClientClick="GetTextBoxValue('<%= rowID.ClientID %>')"/>



function GetTextBoxValue(textBoxID){
   var value = window.document.getElementById(textBoxID).value; }

回答by naveen

Or for cleaner mark-up, bind the event in code-behind at PageLoadlike this

或者为了更清晰的标记,PageLoad像这样在代码隐藏中绑定事件

if (!IsPostBack)
{
    string script = String.Format("GetTextBoxValue('{0}')", rowID.ClientID);
    Click.Attributes.Add("onclick", script);
}

Now your mark-up can be like this.

现在您的标记可以是这样的。

<asp:TextBox ID="rowID" runat="server"> </asp:TextBox>
<asp:Button ID="Click" runat="server" />