开发者

jquery get previous input text

开发者 https://www.devze.com 2023-01-30 02:31 出处:网络
I have the following html: <div class=\"active_string\"> <input type=\"text\" class=\"answer\" /><div class=\"images\"><img src=\"/images/myImage.png\" alt=\"\" /></div>

I have the following 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:

$(this).prev().prev().text()

but this one and other combinations of prev text() and val are not showing the t开发者_如何学运维ext Ive put into that textfield.


Have you tried $(this).prev('input')?

EDIT:

I mean: $(this).prevAll('input').val().


It should work with val().

Here is working demo

Even it is working with

$(this).prevAll("input[type=text]").val()


Try

$(this).prevAll("input[type=text]").val()

Live example


I think

$(this).parent().find('input').val();

would be the best way to go.


$(".check_quest").click(function() {
    var answer = $(this).prevAll("input").first().val();
});


Here is a working example with two inputs that will show you the different values when you click the links


Try this:

HTML Code

<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

$(".check_quest").click(function(event)
 {
     event.preventDefault();
     alert ($("#answer").val())
 });      

Here is a working demo: http://jsfiddle.net/tVcKS/

0

精彩评论

暂无评论...
验证码 换一张
取 消