简体   繁体   English

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

[英]Set HTML textbox value from Javascript function return value

I've two separate HTML file and .js file.我有两个单独的 HTML 文件和 .js 文件。 I have a calculate method which return the result in the Javascript file.我有一个计算方法,它在 Javascript 文件中返回结果。

My question is how to set a textbox value from a Javascript return value.我的问题是如何从 Javascript 返回值设置文本框值。 Or at least from a separate Javascript file.或者至少来自一个单独的 Javascript 文件。 Initially I tried below which doesn't work as Javascript and html are separated.最初我在下面尝试过它不起作用,因为 Javascript 和 html 是分开的。

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

eval is the soution for my problem. eval 是我的问题的解决方案。 Input.value = eval(Calculate(ch)) Input.value = eval(Calculate(ch))

try to define the js-file in the bottom of your html-file (near the </body>).尝试在 html 文件的底部(靠近 </body>)定义 js 文件。 if you defined it above the targeted element its unknown.如果您在目标元素上方定义它,则它是未知的。 this may help.这可能会有所帮助。

felix菲利克斯

Access textarea contents using the innerHTML property, not the value property.使用innerHTML属性而不是value属性访问 textarea 内容。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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