如何获取最近输入Jquery的值
我的页面上有多个具有相同类别的按钮。当我单击按钮时,我想获取最近的输入框的值(请记住我的页面上有多个具有相同类的输入框和按钮)。
我有:
$(".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>
当用户单击链接时,如何获取最近输入的值并使我的变量等于它???
I have multiple buttons on my page with the same class. When I click on a button I want to get the value of the nearest input box (remember there are multiple input boxes and buttons on my page with the same classes).
I have:
$(".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>
How can I get the value of the nearest input and make my variable equal to it when a user clicks on the link????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确定它们就在下一个,请使用
.prev()
彼此。 (这应该是最快的。)这将获取前一个元素。
如果中间可能存在另一个元素,您可以使用
.siblings()
:这将获得具有
deleteid
类的所有同级在同一个中。
Use
.prev()
if you're sure they're right next to each other. (This should be fastest.)This gets the previous element.
If there's a chance that there will be another element in between, you could use
.siblings()
:This will get all siblings inside the same
<div>
with thedeleteid
class.它可能(而且确实)会导致性能问题。例如:
有时它会返回 .deleteid 的先前值(不是 10)
It may (and it does) cause problems with perfomance. For example:
Sometimes it returns the previous value of .deleteid (not 10)