Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用输入字段作为javascript函数的参数?_Javascript_Html_Function_Input - Fatal编程技术网

如何使用输入字段作为javascript函数的参数?

如何使用输入字段作为javascript函数的参数?,javascript,html,function,input,Javascript,Html,Function,Input,所以我有一些输入文本字段和一个按钮 <input type=text value="a"/> <input type=text value="b"/> <input type=button onclick=???/> 我不知道在问号里填什么。因此,获取文本输入值的不是document.getElementById,而是元素本身。为输入分配一个id,然后使用getElementById调用它们 <input type="text" id="field1"

所以我有一些输入文本字段和一个按钮

<input type=text value="a"/>
<input type=text value="b"/>
<input type=button onclick=???/>

我不知道在问号里填什么。因此,获取文本输入值的不是document.getElementById,而是元素本身。

为输入分配一个
id
,然后使用
getElementById
调用它们

<input type="text" id="field1" value="a"/>
<input type="text" id="field2" value="b"/>
<input type=button onclick="foo('field1','field2');"/>

<script type="text/javascript">
    function foo(a,b) {
        elemA = document.getElementById(a).value;
        elemB = document.getElementById(b).value;
        dostuff(elemA);
        dostuff(elemB);
    }
</script>

函数foo(a,b){
elemA=document.getElementById(a).value;
elemB=document.getElementById(b).value;
多斯塔夫(埃莱玛);
多斯塔夫(elemB);
}

有多种方法可以访问这些值,但建议首先给出输入元素ID

<input type=text value="a" id="a"/>
<input type=text value="b" id="b"/>
注意“vs”的用法,因为它们是嵌套的


但是你也可以把ID传递给
foo
,让
foo
getElementById
的东西。

框架被设置为“onLoad”,所以
foo
函数被捕获在一个闭包中,在全局范围内不可用。设置为“no wrap”,你应该很好。不是所有浏览器(如lte 4)支持getElementById。请参阅
<input type=text value="a" id="a"/>
<input type=text value="b" id="b"/>
<input type=button onclick="foo(document.getElementById('a').value,document.getElementById('b').value)" />