removeClass在meteor.js中不起作用

时间:2016-02-23 18:05:08

标签: javascript jquery meteor

我在meteor.js中设置了一个Template.inquiry.events({函数,它接受按钮的点击数据并为它们添加一个类。这工作正常。什么是无效的,是当我尝试让点击删除该类。

看起来像这样:

Template.inquiry.events({
    'click .button': function(event){

        var button_id=event.currentTarget.id

        report = false;
        consult = false;
        contract = false; 

        $("#" + button_id).addClass('selected');

        if(button_id == 'report' && report == false) {
            report = true;
            $("#" + button_id).addClass('selected');
        }
        else if(button_id == 'consult' && consult == false) {
            consult = true;
            $("#" + button_id).addClass('selected');
        }
        else if(button_id == 'contract' && contract == false) {
            contract = true;
            $("#" + button_id).addClass('selected');
        }
        else if(button_id == 'report' && report == true) {
            report = false;
            $("#" + button_id).removeClass('selected');
        }
        else if(button_id == 'consult' && consult == true) {
            consult = false;
            $("#" + button_id).removeClass('selected');
        }
        else if(button_id == 'contract' && contract == true) {
            contract = false;
            $("#" + button_id).removeClass('selected');
        }

    }
});

前三个“ifs”工作正常,但removeClass ifs无效。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

每次点击report consultcontract都是false所以"否则如果"检查report == true无法触发的位置。如果您希望事件之间的商店状态使用SessionReactiveVar或仅Template.instance()属性,请

Template.inquiry.onCreated(function() {
    this.states = {};
    this.states.report = false;
});
...

    else if(button_id == 'consult' && !Template.instance().states.report) {
        Template.instance().states.report = true;
        $("#" + button_id).addClass('selected');
    }
    else if(button_id == 'report' && Template.instance().states.report) {
        Template.instance().states.report = false;
        $("#" + button_id).removeClass('selected');