簡體   English   中英

jQuery觸發click in循環在第一個之后停止

[英]jQuery trigger click in loop stops after the first

我正在嘗試執行此操作,但是循環在第一個觸發器之后結束。 有什么辦法可以幫助我嗎?

$(document).on('click', '#configurator .myalbumimgs .autofill', function(e) {

    e.preventDefault();

    var nb_of_images = $('#grid div.configimg').length;

    for (i = 0; i < nb_of_images; i++) {

        alert(i);

        $('.imgpicker .photo .add').eq(i).trigger('click');
    }

}); 

編輯1:如果我刪除$('。imgpicker .photo .add')。eq(i).trigger('click');一切正常 並讓循環運行。

如果我輸入一個數字,則像eq(3)一樣選擇正確的一個,但只能選擇一個。 然后它像以前一樣停止。

控制台中沒有錯誤:S

編輯2:找出正確的解決方案,我對.eq的錯誤輸入錯誤的元素。 感謝所有建議! 正確的代碼:

$(document).on('click', '#configurator .myalbumimgs .autofill', function(e) {

    e.preventDefault();

    if ($(this).not('.done')) {

        var multiselector_nbimages = $('#grid').attr('data-nbimages');

        var nb_images_selected = parseInt($('#grid div.configimg').not('.temp').length);
        var max_nb_images = parseInt(multiselector_nbimages);

        if (nb_images_selected < max_nb_images) {

            var album_images = $(this).parent().parent().children('.imgpicker').children('.photo');
            var nb_of_grid_images = $('#grid div.configimg.temp').length;

            for (i = 0; i < nb_of_grid_images; i++) {

                album_images.eq(i).children('.add').not('.selected').trigger('click');;
            }

            $(this).addClass('done');

        } else {

            alert(lang_valid_max_nb_of_photos);
        }

    }

});

jQuery內置了每種方法。

$('#configurator .albumbtn.autofill').on('click', function(e) {

e.preventDefault();

//Iterate through every configimg
$('#grid div.configimg').each(function(el) {
// $(this) is current element in the list
   if($(this).hasClass('someclass')) { $(this).trigger('click') }
})

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM