我正试图在单击按钮时获取并显示输入的值。
$('.nextBtn').click(function() {
alert($('this').prevAll('input[type=tel]').val())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="step" id="step1">
<h2>How old are you?</h2>
Answer: <input type="tel" /> years old
<button id="stepBtn1" class="nextBtn">Next</button>
</div>
此处演示:http://jsfiddle.net/r7c1skgg/11/
我确信这里有一些基本的东西我不理解,但prevAll文档似乎没有拼写出答案(我确信这是显而易见的)。
试试这个,在没有定义id的情况下更改解决方案。问题是$(this).prevAll中的报价不是$(this)
$('.nextBtn').click(function() {
alert($(this).prevAll('input[type=tel]').val());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="step" id="step1">
<h2>How old are you?</h2>
Answer: <input type="tel"/> years old
<button id="stepBtn1" class="nextBtn">Next</button>
</div>