简体   繁体   English

在点击处理程序中调用函数,未运行

[英]Calling a function inside click handler, not running

Problem 问题

I'm calling the function resetSelection that should remove .is-active classes and add a class of is-inactive to a list of hockey players, amongst other things, after the submit button .btn--submit has been clicked. 我正在调用函数resetSelection ,该函数应该删除resetSelection .is-active类,并在单击提交按钮.btn--submit之后,向曲棍球运动员列表中添加is-inactive类。

Right now, the .is-active classes remain on the picked players and the function does not run? 现在, .is-active类仍保留在选择的播放器上,并且该功能无法运行?

index.html index.html

<button class="btn btn--submit"><img src="src/img/ballot-alt.png" class="image--ballot">Submit Vote</button>

scripts.js scripts.js

function resetSelection() {
    $(".btn--reset").on("click", function(){
        $(".picked").removeClass("is-active");
        $(".picked").addClass("is-inactive");
        $(".icon-checkmark").removeClass("is-completed");
        $(".btn--submit").hide();
        $(".btn--submit").removeClass("slideLeft");
        starredGoaltenders = 0;
        starredDefencemen = 0;
        starredForwards = 0;
        console.log(starredGoaltenders, starredDefencemen, starredForwards);
    });
} resetSelection();

$(".btn--submit").on("click", function(){
    console.log("Clicked");
    resetSelection();
});

Your function binds a click handler event to elements with the class btn--reset . 您的函数将单击处理程序事件绑定到具有btn--reset类的元素。 Consider refactoring resetSelection to include the logic within that event handler, but not the event handler itself: 考虑重构resetSelection以在该事件处理程序中包括逻辑,但不包括事件处理程序本身:

function resetSelection() {
    $(".picked").removeClass("is-active");
    $(".picked").addClass("is-inactive");
    $(".icon-checkmark").removeClass("is-completed");
    $(".btn--submit").hide();
    $(".btn--submit").removeClass("slideLeft");
    starredGoaltenders = 0;
    starredDefencemen = 0;
    starredForwards = 0;
    console.log(starredGoaltenders, starredDefencemen, starredForwards);
}

$(".btn--reset").on("click", resetSelection);

$(".btn--submit").on("click", function(){
    console.log("Clicked");
    resetSelection();
});

Your reselectSelection() function contains an click event handler for .btn--reset . 您的reselectSelection()函数包含.btn--reset的click事件处理程序。 Remove that handler and it should work for you because you want to trigger reset function when .btn--submit is clicked. 删除该处理程序,它应该对您.btn--submit因为您想在单击.btn--submit时触发重置功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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