2016-08-18 1 views

答えて

0

$(this).next()

$("input").on("keydown", function(){ 
 
    $(this).next('.error').html('Becareful Baby!'); 
 
    $(this).closest('br').next('.error').html('Becareful Baby!'); 
 
    $(this).closest('.error').html('Becareful Baby!'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" name="fname" id="fname" class="nametxt" /> 
 
<br /> 
 
<span class="error"></span>
$(this).next('.error').html('Becareful Baby!'); 
$(this).closest('br').next('.error').html('Becareful Baby!'); 
$(this).closest('.error').html('Becareful Baby!'); 

はあなたが選択した1後の要素を取得します。セレクタを指定すると結果がさらに絞り込まれます。あなたの入力要素の後の要素は <br/>なので、それは .next()があなたに与えるものです。

$(this).siblings('.error')をお勧めします。ここに例があります:https://jsfiddle.net/77nvepos/1/

+0

こんにちはジェイクですが、これはすべての可能な既存の '.error' –

関連する問題