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

Pass a javascript variable to html. jQuery - Stack Overflow

programmeradmin0浏览0评论

I have a variable in Javascript:

var test ="whatever"

I just want to pass this variable inside a hidden input:

<input type="hidden" class="myinput" value="">

I tried:

$('.myinput').attr('test');

But it does not seem to work.

Thank you for your help.

I have a variable in Javascript:

var test ="whatever"

I just want to pass this variable inside a hidden input:

<input type="hidden" class="myinput" value="">

I tried:

$('.myinput').attr('test');

But it does not seem to work.

Thank you for your help.

Share Improve this question edited Nov 12, 2012 at 22:48 Nat Ritmeyer 5,6788 gold badges48 silver badges59 bronze badges asked Nov 12, 2012 at 22:43 Juliette DupuisJuliette Dupuis 1,0372 gold badges14 silver badges22 bronze badges 1
  • Did it help, if so please mark it is as accepted for closure. – Selvakumar Arumugam Commented Nov 13, 2012 at 20:00
Add a ment  | 

1 Answer 1

Reset to default 8
  1. Use .val function to set the value of hidden input.
  2. Remove the quotes to consider it as a variable and not a string literal.

See below code:

$('.myinput').val(test);
发布评论

评论列表(0)

  1. 暂无评论