in index.php
<input type="hidden" name="id" value="1" />
<input type="hidden" name="type" value="2" />
in script.js
$(document).ready(function(){
$('#button').click(function(){
var id = $(this).prev('input[type="hidden"][name="id"]').val();
var type = $(this).prev('input[type="hidden"][name="type"]').val();
alert(id);
alert(type);
});
when I run file index.php, result is undefined, how to fix it
prev()only gets the imminently preceding element and at least the “id” element doesn’t precede the button.I’m guessing the button and the inputs share the same parent, so you probably could use:
You’ll need to add the surrounding HTML and the HTML of the button if this doesn’t work.
EDIT: Another alternative maybe to use
prevAll()instead ofprev().