2016-06-23 4 views
1

У меня возникла проблема с выбором следующей кнопки отправки через jQuery.Trigger next submit button

У меня есть что-то вроде этого:

$('.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() как this является textarea и не div и у вас есть <div> в между textarea и button, так .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'); 
}); 

Рабочая 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>