2016-06-23 4 views
1

jQueryを使用して次の送信ボタンを選択する際に問題があります。次の送信ボタンをトリガーする

私はこのようなものがあります:thistextareaないdivであり、あなたがtextareabuttonの間で<div>、そう.next()ウォンを持っているとしてだけでnextAll()を使用

$('.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

をいずれかの作業:

$('.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>

関連する問題