1

I hava just a simple issue .

I wanna assign a value to a text box , but not as a string , as Java script function .

I will give you simple example :

     <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>

now after this simple example , my question is : how can i assign a value to the text box using java script function ?

4

1 回答 1

5

You need to assign it in the script, you cannot call a function in the value attribute of the html.

Try this:

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

Demo here

My suggestion is to give a id to the input (like <input id="input_id" type="text" value="exam">) and then use getElementByID instead in my example.

In this case it could look like this demo.

于 2013-09-22T08:22:34.810 回答