触发下一个提交按钮

时间:2016-06-23 16:30:52

标签: javascript jquery

我在通过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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

只需使用nextAll(),因为thistextarea而不是div<div>和{之间有textarea {1}},所以button也不会工作:

.next()

或者您可以这样使用$('.description-part').on('change', 'textarea', function() { $(this).nextAll('.button').addClass('test'); });

.closest()

工作代码段

&#13;
&#13;
$('.description-part').on('change', 'textarea', function() {
    $(this).closest('.description-part').find('.button').addClass('test');
});
&#13;
$(function() {
  $('.description-part').on('change', 'textarea', function() {
    $(this).closest('.description-part').find('.button').addClass('test');
  });
});
&#13;
.test {background: red;}
&#13;
&#13;
&#13;

&#13;
&#13;
<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>
&#13;
$(function() {
  $('.description-part').on('change', 'textarea', function() {
    $(this).nextAll('.button').addClass('test');
  });
});
&#13;
.test {background: red;}
&#13;
&#13;
&#13;

相关问题