从 Javascript 函数返回值设置 HTML 文本框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5351789/
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-08-23 16:47:58  来源:igfitidea点击:

Set HTML textbox value from Javascript function return value

javascripthtml

提问by CharithJ

I've two separate HTML file and .js file. I have a calculate method which return the result in the Javascript file.

我有两个单独的 HTML 文件和 .js 文件。我有一个计算方法,它在 Javascript 文件中返回结果。

My question is how to set a textbox value from a Javascript return value. Or at least from a separate Javascript file. Initially I tried below which doesn't work as Javascript and html are separated.

我的问题是如何从 Javascript 返回值设置文本框值。或者至少来自一个单独的 Javascript 文件。最初我在下面尝试过它不起作用,因为 Javascript 和 html 是分开的。

function Calculate(ch) 
{
    //...
    document.getElementById('Input').value = resultValue;
}

采纳答案by CharithJ

eval is the soution for my problem. Input.value = eval(Calculate(ch))

eval 是我的问题的解决方案。Input.value = eval(Calculate(ch))

回答by Felix Geenen

try to define the js-file in the bottom of your html-file (near the </body>). if you defined it above the targeted element its unknown. this may help.

尝试在 html 文件的底部(靠近 </body>)定义 js 文件。如果您在目标元素上方定义它,则它是未知的。这可能会有所帮助。

felix

菲利克斯

回答by glomad

Access textarea contents using the innerHTMLproperty, not the valueproperty.

使用innerHTML属性而不是value属性访问 textarea 内容。

function Calculate(ch)  {
    //...
    document.getElementById('Input').innerHTML = resultValue;
}