如何将文本框中的文本传递给JavaScript函数? [英] How to pass text in a textbox to JavaScript function?

查看:91
本文介绍了如何将文本框中的文本传递给JavaScript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下HTML代码,如何传递用户输入来执行(str)JavaScript函数作为参数?

 <身体GT; 

< input name =textbox1type =text/>
< input name =buttonExecuteonclick =execute(//将用户在textbox1中的输入发送给此函数//)type =buttonvalue =Execute/>

< / body>


解决方案

您可以通过名称访问元素的值:

  document.getElementsByName(textbox1); //返回名称为textbox1的元素列表
document.getElementsByName(textbox1)[0] //返回DOM中名称=textbox1的第一个元素






$ p $ <输入

name =buttonExecuteonclick =execute(document.getElementsByName('textbox1')[0] .value)type =buttonvalue =Execute/>

或者您为元素指定一个ID,然后标识它,您可以使用 getElementById

 < input name =textbox1id =textbox1type =text/> 
< input name =buttonExecuteonclick =execute(document.getElementById('textbox1')。value)type =buttonvalue =Execute/>


Suppose I have the following HTML code, how can I pass the user's input to execute(str) JavaScript function as an argument?

<body>

<input name="textbox1" type="text" />
<input name="buttonExecute" onclick="execute(//send the user's input in textbox1 to this function//)" type="button" value="Execute" />

</body>

解决方案

You could either access the element’s value by its name:

document.getElementsByName("textbox1"); // returns a list of elements with name="textbox1"
document.getElementsByName("textbox1")[0] // returns the first element in DOM with name="textbox1"

So:

<input name="buttonExecute" onclick="execute(document.getElementsByName('textbox1')[0].value)" type="button" value="Execute" />

Or you assign an ID to the element that then identifies it and you can access it with getElementById:

<input name="textbox1" id="textbox1" type="text" />
<input name="buttonExecute" onclick="execute(document.getElementById('textbox1').value)" type="button" value="Execute" />

这篇关于如何将文本框中的文本传递给JavaScript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆