javascript - Retrieving hidden input value using jQuery - Stack Overflow

I have a hidden input in the manner below:<div id="message"><input id="hiddenin

I have a hidden input in the manner below:

<div id="message">
    <input id="hiddeninput" type="hidden">
    <span>Message with submit button <input type=button id="confirm" value="Submit"></span>  
</div>

The hidden input is given a value after a jQuery POST. I need to retrieve the value that is set, and send it in another jQuery POST.

Interestingly, I get this:

<input id="hiddeninput" type="hidden">34345</input>

after fetching the value from the server in the first jQuery post.

Just $("#hiddeninput").val() does not retrieve the value which I want to send.

What is the correct way to do it in my example?

EDIT: In JQuery, This is how I set the value to the hidden field:

$.post("post.php", function(data){


    if(data.length > 0){
    var resultObj =  eval(data)[0];
    if(resultObj.SomeNumber >= 0)
    {
        $("#hidden").html(resultObj.SomeNumber);
    }
 });

I have a hidden input in the manner below:

<div id="message">
    <input id="hiddeninput" type="hidden">
    <span>Message with submit button <input type=button id="confirm" value="Submit"></span>  
</div>

The hidden input is given a value after a jQuery POST. I need to retrieve the value that is set, and send it in another jQuery POST.

Interestingly, I get this:

<input id="hiddeninput" type="hidden">34345</input>

after fetching the value from the server in the first jQuery post.

Just $("#hiddeninput").val() does not retrieve the value which I want to send.

What is the correct way to do it in my example?

EDIT: In JQuery, This is how I set the value to the hidden field:

$.post("post.php", function(data){


    if(data.length > 0){
    var resultObj =  eval(data)[0];
    if(resultObj.SomeNumber >= 0)
    {
        $("#hidden").html(resultObj.SomeNumber);
    }
 });
Share Improve this question edited Jan 18, 2011 at 10:06 Naweed Chougle asked Jan 18, 2011 at 8:37 Naweed ChougleNaweed Chougle 5201 gold badge10 silver badges32 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

You have to set the value of the hidden field like this, then it should work

<input id="hiddeninput" type="hidden" value="34345" />

The hidden element

<input id="hiddeninput" type="hidden">

does not have the value attribute. It should be like

<input id="hiddeninput" type="hidden" value="someValue"> 

$("#hiddeninput").html() would retrieve 34345 from the structure as you show although as stated above the value attribute should be used on a hidden field and then val() will work.

Since <input id="hiddeninput" type="hidden">34345</input> is not the "right" way to format the input tag,a s opposed to <input id="hiddeninput" type="hidden" value="34345"/> you need to use $("#hiddeninput").text()

try explicitly adding the value tag to the input elemeent before the post adds it i.e.

<input id="hiddeninput" type="hidden" value="">

If that doeesn't work have a look at this thread: jquery selector can't read from hidden field

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745263121a4619300.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信