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();
4

1 に答える 1