2016-06-23 103 views
1

我遇到了通过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>

回答

3

只需使用nextAll()作为thistextarea,而不是div,你在textareabutton之间有<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>