jquery 获取上一个输入文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4407614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery get previous input text
提问by Yaroslav Yakovlev
I have the following html:
我有以下 html:
<div class="active_string">
<input type="text" class="answer" /><div class="images"><img src="/images/myImage.png" alt="" /></div>
<a href="" class="check_quest" id="1">Click me</a>
</div>
<div class="active_string">
<input type="text" class="answer" /><div class="images"><img src="/images/myImage.png" alt="" /></div>
<a href="" class="check_quest" id="2">Click me</a>
</div>
For every click on check_quest I want to get value of a corresponding input. How can I do it using query. I`ve tried the following:
每次点击 check_quest 我都想获得相应输入的值。我如何使用查询来做到这一点。我尝试了以下方法:
$(this).prev().prev().text()
but this one and other combinations of prev text() and val are not showing the text Ive put into that textfield.
但是这个和 prev text() 和 val 的其他组合没有显示我放入该文本字段的文本。
回答by Spiny Norman
Have you tried $(this).prev('input')
?
你试过$(this).prev('input')
吗?
EDIT:
编辑:
I mean: $(this).prevAll('input').val()
.
我的意思是:$(this).prevAll('input').val()
。
回答by Chinmayee G
It should work with val().
它应该与 val() 一起使用。
Here is working demo
这是 working demo
Even it is working with
即使它正在与
$(this).prevAll("input[type=text]").val()
回答by T.J. Crowder
回答by Strom
$(".check_quest").click(function() {
var answer = $(this).prevAll("input").first().val();
});
回答by Haralan Dobrev
I think
我认为
$(this).parent().find('input').val();
would be the best way to go.
将是最好的方式去。
回答by Nalum
回答by Jan
Try this:
尝试这个:
HTML Code
HTML代码
<div class="active_string">
<input type="text" id="answer" />
<div class="images"><img src="/images/myImage.png" alt="" /></div>
<a href="" class="check_quest" id="1">Click me</a>
</div>
JavaScript
JavaScript
$(".check_quest").click(function(event)
{
event.preventDefault();
alert ($("#answer").val())
});
Here is a working demo: http://jsfiddle.net/tVcKS/
这是一个工作演示:http: //jsfiddle.net/tVcKS/