Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何在一个页面中以多种形式获取不同的隐藏字段值_Jquery - Fatal编程技术网

Jquery 如何在一个页面中以多种形式获取不同的隐藏字段值

Jquery 如何在一个页面中以多种形式获取不同的隐藏字段值,jquery,Jquery,在一个页面中,我有两个表单,其中两个表单都有一个名为operation的隐藏字段(两个表单的名称相同) 包括jquery.min-1.5.js和jquery-ui.min-1.8.js $(文档).ready(函数(){ $('get#val').live('click',function(){ 警报($(“#操作”).val(); }); }); //一些代码。。。。。 //一些代码。。。。。 当我单击“Get Val”按钮时,我总是得到“first”(对于这两种情况)。但我需要我单击的按

在一个页面中,我有两个表单,其中两个表单都有一个名为operation的隐藏字段(两个表单的名称相同)

包括jquery.min-1.5.js和jquery-ui.min-1.8.js
$(文档).ready(函数(){
$('get#val').live('click',function(){
警报($(“#操作”).val();
});
});
//一些代码。。。。。

//一些代码。。。。。

当我单击“Get Val”按钮时,我总是得到“first”(对于这两种情况)。但我需要我单击的按钮的值。

//在这两种表单中使用唯一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>
参考

您可以尝试以下方法:

$(document).ready(function(){
$("input[name=get_val]").bind({
    click: function(event) {
       alert($("input[name=operation]" , $(this).parent()).val());
    }
});
});

如果使用
prev()
,则仅获取上一个元素的值。如果按钮的上一个元素是另一个元素(不是操作),则您将获得错误的值。

由于某些原因,我无法重命名。我希望使用表单名称获取值。谢谢,但我无法更改名称或id。是否有任何方法可以获取表单名称、隐藏字段名称之类的值?如果前一个是另一个元素(非操作),那么您将获得错误的值。因此,我认为在获得值之前,我们应该使用按名称或id查找元素。现在,您可以通过更新结果来实现这一点,无需更改id,并感谢focus@ha nguyen
 $('input[name=get_val]').live('click', function() {
    alert($(this).prev().val());
  });
$(document).ready(function(){
$("input[name=get_val]").bind({
    click: function(event) {
       alert($("input[name=operation]" , $(this).parent()).val());
    }
});
});