[英]Trigger next submit button
I am having problem with selecting the next submit button via jQuery. 我通过jQuery选择下一个提交按钮时遇到问题。
I have something like this: 我有这样的事情:
$('.description-part').on('change', 'textarea', function() { $(this).find('.button').addClass('test'); // not working $(this).next('.button').addClass('test'); // not working $(this).closest('.button').addClass('test'); // not working });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="description-part"> ... <textarea></textarea> <div></div> <input type="submit" name="save" class="button"> ... </div>
Just use the nextAll()
as the this
is textarea
and not div
and you have a <div>
in between the textarea
and button
, so .next()
won't work either: 只需使用
nextAll()
因为this
是textarea
而不是div
,你在textarea
和button
之间有一个<div>
,所以.next()
也不会工作:
$('.description-part').on('change', 'textarea', function() {
$(this).nextAll('.button').addClass('test');
});
Or you can use .closest()
this way: 或者你可以这样使用
.closest()
:
$('.description-part').on('change', 'textarea', function() {
$(this).closest('.description-part').find('.button').addClass('test');
});
Working Snippet 工作片段
$(function() { $('.description-part').on('change', 'textarea', function() { $(this).closest('.description-part').find('.button').addClass('test'); }); });
.test {background: red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="description-part"> ... <textarea></textarea> <div></div> <input type="submit" name="save" class="button" />... </div>
$(function() { $('.description-part').on('change', 'textarea', function() { $(this).nextAll('.button').addClass('test'); }); });
.test {background: red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="description-part"> ... <textarea></textarea> <div></div> <input type="submit" name="save" class="button" />... </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.