我正在尝试获取每个列表的大小,因为有多个列表并且每个列表都被动态添加。它似乎没有返回正确的数字,不知道为什么。
计数应返回每个部分的列表项数
这里也有一个小提琴:http: //jsfiddle.net/Dqf8B/
<section id="questionOne">
<span class="surveyQuestion"></span>
<form>
<input>
<a class="button">Add</a>
</form>
<p class="helperText">drag to prioritize</p>
<ol class="draggable answers">
</ol>
</section>
<section id="questionTwo">
<span class="surveyQuestion"></span>
<form>
<input>
<a class="button">Add</a>
</form>
<p class="helperText">drag to prioritize</p>
<ol class="draggable answers">
</ol>
</section>
function checkIfHelperIsNeeded(counter) {
if(counter == 2){
helperText.slideToggle(function(){
$(this).fadeIn();
})
if (counter < 2) { helperText.hide(); };
}
}
$('.button').each(function(){
var counter = $(this).parent("form").parent("section").find("ol").size();
console.log(counter);
$(this).click( function(){
if(counter <= 5) {
checkIfHelperIsNeeded(counter);
var myCurrentInputTags = $(this).parent('form').children('input').val();
var li = $('<li class="tag">'+myCurrentInputTags+'</li>');
$('<span class="close"></span>').on('click', removeParent).prependTo(li);
$(this).parent('form').parent('section').children('.draggable').prepend(li);
} else { alert("only 5 answers per question allowed")}
});
})
function removeParent() {
$(this).parent().fadeOut( function() {
$(this).remove();
});
}
var helperText = $(".helperText");
helperText.hide();