Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.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 使用java脚本为文本框赋值_Javascript_Html - Fatal编程技术网

Javascript 使用java脚本为文本框赋值

Javascript 使用java脚本为文本框赋值,javascript,html,Javascript,Html,我有一个简单的问题 我想给文本框赋值,但不是作为字符串,而是作为Java脚本函数 我给你举个简单的例子: <input type="text" value="exam">// here i assign the value as a string . <input type="text" value=functionx() > // functionx is a java script function that

我有一个简单的问题

我想给文本框赋值,但不是作为字符串,而是作为Java脚本函数

我给你举个简单的例子:

     <input type="text" value="exam">//  here i assign the value as a string .





    <input type="text" value=functionx() >  // functionx is a java script function that        
          will assign a value to the text box.

     <script > function functionx(){return 'exam';}</script>
//这里我将值指定为字符串。
//functionx是一个java脚本函数
将为文本框指定一个值。
函数functionx(){返回'exam';}

在这个简单的示例之后,我的问题是:如何使用java脚本函数为文本框赋值

您需要在脚本中分配它,不能在html的value属性中调用函数

试试这个:

var text = document.getElementsByTagName('input')[0]; // would be better if you have a ID
text.value = functionx();
演示

我的建议是给输入一个
id
(比如
),然后在我的示例中使用
getElementByID


在这种情况下,它可能看起来像。

您需要在脚本中分配它,您不能在html的value属性中调用函数

试试这个:

var text = document.getElementsByTagName('input')[0]; // would be better if you have a ID
text.value = functionx();
演示

我的建议是给输入一个
id
(比如
),然后在我的示例中使用
getElementByID


在这种情况下,它可能看起来像。

这是完美的;是的,不用担心ID,在我的代码中当然有ID,但这是一个例子,你们知道。这是正确的答案,thanx。这是完美的;是的,不用担心ID,在我的代码中当然有ID,但这是一个例子,你们知道。这是正确的答案,坦克斯。