How do I reference the value of an input in jQuery?
I'm currently using the following method to return a value from a div:
<div data-post-id="1">
<a>Add To Favourites</a>
<div>
$(this).parent().data('post-id');
What I would like to do is something like this:
<div>
<input type="hidden" name="postId" value="1" />
<a>Add To Favourites</a>
<div>
Keep in mind:
There will be several posts on each page. Each div containi开发者_运维知识库ng a post will have it's own input.
The value of the postId will not be accessible from my JS file. So the its reference has to be retrieved via JavaScript somehow.
I want to get away from using parent() and use a single reference for each post's id, regardless whether the action is to favourite, send to a friend, etc...
to get the val of postId use
alert($("input#postId").val());
You could use an attribute selector to get the hidden inputs. Just add a "isPostId" field to your hidden input and then grab them with the jQuery object:
$('div input[isPostId="true"]')
<input type="hidden" isPostId="true" value="1"/>
精彩评论