admin管理员组

文章数量:1323736

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

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.

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

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

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.

function Calculate(ch) 
{
    //...
    document.getElementById('Input').value = resultValue;
}
Share Improve this question edited May 25, 2020 at 9:15 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 18, 2011 at 12:11 CharithJCharithJ 47.6k20 gold badges126 silver badges137 bronze badges 3
  • is 'Input' the id of the textbox? – Tim B James Commented Mar 18, 2011 at 12:14
  • Try using an alert box with your method in it. Also setting up a simple example on a site like jsfiddle helps us out a lot. – Timothy Ruhle Commented Mar 18, 2011 at 12:19
  • can you put up the relevant part of the HTML file? – Dave Everitt Commented Mar 18, 2011 at 12:29
Add a ment  | 

3 Answers 3

Reset to default 1

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

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.

felix

Access textarea contents using the innerHTML property, not the value property.

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

本文标签: Set HTML textbox value from Javascript function return valueStack Overflow