简体   繁体   中英

How can I pass value(contents) of HTML textbox to a javascript function on a button click?

I have a java script function

function findInthePage(str)
{
// code
}

How can I pass the contents of a HTML text box to the above function? My button click code is

<input id="Text1" type="text" name="tb1" />
<input id="Button1" type="button" value="button" onclick="findInthePage(text1.value);" />

尝试这个:

var text = document.getElementById('Text1').value

The value property is used with Form elements, it gets or sets values to fields of the form.

  • To use "getElementById('id')" with form elements, they must have assigned an "id". Here's a simple example that displays an Alert with the text written in a text box.

    function test7() { var val7 = document.getElementById("ex7").value; alert(val7); } html

    // input type="text" id="ex7"

    // input type="button" value="Click" onclick="test7()"

在此处输入图片说明

You can do:

var textBox = document.getElementById("Text1");
findInthePage(textBox.value);

尝试:

<input id="Button1" type="button" value="button" onClick="findInthePage(document.getElementById('Text1').value)" />

You can access input directly from the function like:

function findInthePage()
{
// code

var input = document.getElementById('Button1').value;


}

If you want your way:

<input id="Text1" type="text" name="tb1" />
<input id="Button1" type="button" value="button" onclick="findInthePage(document.getElementById('Button1').value);" />

Try this

var value = null;
document.getElementById("Button1").onclick = function(){
    value = document.getElementById("Text1").value;
};
<input id="txtbox" type="text" name="tb1" />
<input id="Button1" type="button" value="button" onclick="clickme();"/>

script
function clickme() {
    var aa = document.getElementById("txtbox").value;
    alert(aa);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM