私のプロジェクトでは。div
親divに存在する他の隣接する番号に特定の番号がすでに存在するかどうかを確認しようとしています。
//generated dynamically using jQuery
<div> //parent div
//many div's
//one of the child div is selected by the user.
</div>
<div> //another parent div
//my function is checking one of these div's instead of the above, rarely
</div>
以下のコードを使用しました。しかし、時々(めったに)div
チェックしたくない別の親の子要素を選択します(私はクロームのデバッガーでチェックしました)。また、関数が正しい親divを選択した場合でも、ユーザーが選択したのと同じ子div要素を常に選択している場合があります。
jQuery
function checkInGroup(){
var selectedDiv = $('#' + _selectedDivId); //updated after engineer's comment
selectedDiv.parent("div").children().each(function(){
debugger;
if(this !== selectedDiv[0]){ //selecting the same div
var self = $(this);
if(self.text() == _randomNumber){
debugger; //never executing
//do some function here
}
}
});
}
編集: jsbinで正常に動作しているのに、なぜコードが動作しないのかわかりません。なぜそうなのかを確認する必要があります。常に同じユーザーが選択したdivで確認していますか?したがって、関数はifステートメントに入りません。
debugger
ifステートメント内に存在するwhichは実行されません。また、各関数ループは、同じ選択されたdivで1回だけ発生します(したがって、関数はifステートメントに入力されません)
これがjsfiddleです