最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

pass a value from javascript to html - Stack Overflow

programmeradmin3浏览0评论

I have a hidden field

<input type="hidden" name="smname" />

Now i have a function in a js file count(); How can I assign the return value to the hidden field?

I have a hidden field

<input type="hidden" name="smname" />

Now i have a function in a js file count(); How can I assign the return value to the hidden field?

Share Improve this question asked Feb 18, 2012 at 12:38 user1199657user1199657
Add a comment  | 

4 Answers 4

Reset to default 14

You could give your hidden field an unique id:

<input type="hidden" name="smname" id="smname" />

and then in javascript:

document.getElementById('smname').value = count();

where count is the function you want to invoke and which returns the value:

var count = function() {
    return 'some value';    
};

Try:

document.getElementsByName('smname')[0].value = count();

You want to use code such as

document.getElementById("name")

then deal with it as an object. remember to define a name/id for your html element.

If you give the input element an id attribute you will be able to get a reference to it in javascript using the document.getElementById() method then set the value property of the element

HTML

<input id="surname" type="hidden" name="smname" />

Javascript

var input = document.getElementById("surname");
input.value = count();
发布评论

评论列表(0)

  1. 暂无评论