Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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_Closest - Fatal编程技术网

如何获取最近输入Jquery的值

如何获取最近输入Jquery的值,jquery,closest,Jquery,Closest,我在同一个类的页面上有多个按钮。当我点击一个按钮时,我想得到最近的输入框的值(记住,我的页面上有多个输入框和按钮,它们具有相同的类) 我有: $(".deletepostbutton").click(function() { var deleteid = $(this).closest('.deleteid').attr('value'); }); <div class="microblogpostactions"> <in

我在同一个类的页面上有多个按钮。当我点击一个按钮时,我想得到最近的输入框的值(记住,我的页面上有多个输入框和按钮,它们具有相同的类)

我有:

    $(".deletepostbutton").click(function() {

            var deleteid = $(this).closest('.deleteid').attr('value');

  });


<div class="microblogpostactions">
     <input type="text"  name="deleteid" class="deleteid" value="'.$row['id'].'" />
     <a href="Javascript:void[0]" class="deletepostbutton">Delete</a>
     </div>

<div class="microblogpostactions">
     <input type="text"  name="deleteid" class="deleteid" value="'.$row['id'].'" />
     <a href="Javascript:void[0]" class="deletepostbutton">Delete</a>
     </div>
$(.deletepostbutton”)。单击(函数(){
var deleteid=$(this).closest('.deleteid').attr('value');
});
当用户单击链接时,如何获取最近输入的值并使我的变量等于该值???

如果您确定它们彼此相邻,请使用。(这应该是最快的。)

这将获取上一个元素

如果中间可能有其他元素,您可以使用:

这将使用
deleteid
类在同一
中获取所有同级

$(this).parent().find('.deleteid').attr('value');
它可能(而且确实)导致性能问题。例如:

$(this).parent().find('.deleteid').val(10);
alert($(this).parent().find('.deleteid').val());
有时它返回.deleteid的上一个值(不是10)

$(this).parent().find('.deleteid').attr('value');
$(this).parent().find('.deleteid').attr('value');
$(this).parent().find('.deleteid').val(10);
alert($(this).parent().find('.deleteid').val());