在一个页面中我有2个表单,其中两个表单都有一个名为operation的隐藏字段(两个表单的名称相同)
included jquery.min-1.5.js & jquery-ui.min-1.8.js
<script>
$(document).ready(function(){
$('#get_val').live('click', function() {
alert($('#operation').val());
});
});
</script>
<form id="form_como" name="form_como" action="go.php" method="post">
//some code.....
<input type="hidden" name="operation" id="operation" value="first">
<input type="button" name="get_val" id="get_val" value="Get Val"/>
</form>
<br />
<form id="form_como2" name="form_como2" action="" method="post">
//some code.....
<input type="hidden" name="operation" id="operation" value="second">
<input type="button" name="get_val" id="get_val" value="Get Val"/>
</form>
当我点击“Get Val”按钮时,我总是得到“第一个”(对于这两种情况)。但是我需要我点击的按钮的值。
答案 0 :(得分:0)
//在两种形式
中使用唯一ID<form id="form_como" name="form_como" action="go.php" method="post">
//some code.....
<input type="hidden" name="operation" id="operation" value="first">
<input type="button" name="get_val" id="get_val" value="Get Val"/>
</form>
<br />
<form id="form_como2" name="form_como2" action="" method="post">
//some code.....
<input type="hidden" name="operation2" id="operation2" value="second">
<input type="button" name="get_val2" id="get_val2" value="Get Val"/>
</form>
更新如果无法更改ID,则按名称使用
$('input[name=get_val]').live('click', function() {
alert($(this).prev().val());
});
参考 prev
答案 1 :(得分:0)
你可以试试这个:
$(document).ready(function(){
$("input[name=get_val]").bind({
click: function(event) {
alert($("input[name=operation]" , $(this).parent()).val());
}
});
});
如果您使用prev()
,那么您只能获得前一个元素的值。如果按钮的前一个元素是另一个元素(不是操作),那么您将得到错误的值。