简体   繁体   中英

How do I get the value of an input field with jQuery

I am trying to set the inner html value of a field and that works fine but when I try to retrieve it it fails.

My code:

            var butId = buttonPressed.getAttribute('id');

            $('#will'+butId).remove();
            $('#hidDelete').html(butId);     //THIS WORKS FINE
            var temp = $('#hidDelete').html();   //THIS DOESNT
            alert (temp);      //THIS PRINTS NOTHING

My input field:

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

Can anyone see whats wrong with it?

尝试$('#hidDelete').val()而不是html()

$().html() is for accessing content from within a pair of tags, eg. <div>foo</div> . You want $().val() . That's for getting/setting the 'value' of input fields.

$('#hidDelete').html(butId);     //THIS WORKS FINE
var temp = $('#hidDelete').html();   //THIS DOESNT
alert (temp);      //THIS PRINTS NOTHING

Instead of above use below

$('#hidDelete').val(butId);     
var temp = $('#hidDelete').val();  
alert (temp); 

in jquery selector.html is used to get the inner html of elements. This should not be used for inputs type elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM