我尝试使用jQuery更改选择字段后获取输入的值。
<div class="box">
<select class="howmuch">
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
<div class="box">
<p>Some Text</p>
</div>
<div class="box">
<input class="entertext" type="text" placeholder="type something" />
</div>
如果用户更改了选择输入(class =多少),我想获取下一个文本输入(class = entertext)的值。
<script>
$(".howmuch").change(function() {
var fetchtext = $(this).nextAll(".entertext:first").val();
// also tried .nextall("input:first")
});
</script>
总是返回“ undefined” 有人可以帮忙吗?
答案 0 :(得分:1)
正确的旅行方式是使用parent(),nextAll()和find()查找所需的输入值。
$(".howmuch").change(function() {
var fetchtext = $(this).parent().nextAll().find(".entertext:first").val();
console.log(fetchtext);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box">
<select class="howmuch">
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
<div class="box">
<p>Some Text</p>
</div>
<div class="box">
<input class="entertext" type="text" placeholder="type something" />
</div>