[英]Trigger next submit button
我通过jQuery选择下一个提交按钮时遇到问题。
我有这样的事情:
$('.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>
只需使用nextAll()
因为this
是textarea
而不是div
,你在textarea
和button
之间有一个<div>
,所以.next()
也不会工作:
$('.description-part').on('change', 'textarea', function() {
$(this).nextAll('.button').addClass('test');
});
或者你可以这样使用.closest()
:
$('.description-part').on('change', 'textarea', function() {
$(this).closest('.description-part').find('.button').addClass('test');
});
工作片段
$(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.