简体   繁体   中英

trigger('click') doesn't work on anchors without an actuall link in the href ( js function in the href attrbute )

i have some links for confirming comments

<a class="confirm_btn" href="javascript:confirm_ajax(17)" id="confirm_17">Confirm</a>
<a class="confirm_btn" href="javascript:confirm_ajax(20)" id="confirm_20">Confirm</a>

i want to be able to confirm all at once with one click , i know it's probably better to get all ids in an array and send them with one ajax call to backend script but for some reason i prefer not to do that and click each button .

here is my jq code

function confirm_all(){
        $('.confirm_btn').each(function(index, element) {
                   $(this).trigger('click');
                  // also i've tried  $(this).click();
            console.log($(this).attr('id'));
        });
}

when i run this i get the console.log result

confirm_17
confirm_20
confirm_22
confirm_33
confirm_34

but the click part doesn't work , it suppose to fire confirm_ajax function ... no error in the firebug .... if i click on the buttons they work fine

trigger('click') will only invoke attached event handlers; if you have JavaScript href s, they won't be triggered.

You could try attaching regular click handlers to your links, or you could do something like this instead:

var idFormat = /confirm_(\d+)/;
$('.confirm_btn').each(function() {
    var btn = $(this);
    var id = parseInt(idFormat.exec(btn.attr('id'))[1], 10);
    confirm_ajax(id);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM