0
我想獲取每個列表中的列表大小,因爲有多個列表,每個列表都被動態添加。它似乎沒有返回正確的數字,不知道爲什麼。動態獲取每個方法中的列表大小
計數應返回的每節
也有一個小提琴在這裏設置列表項的數目: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();