最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbone - Checkbox stays checked? - Stack Overflow

programmeradmin6浏览0评论

I'm trying to hook up a checkbox to my View, but as soon as I tick it, it stays checked, even when I click it again?

Here's part of the View:

views.PaginatedView = Backbone.View.extend({
    events: {
        'click inputpletedEnquiries': 'filterCompletedEnquiries'
    },
    filterCompletedEnquiries: function (e) {
        return e.currentTarget.checked;
    }
});

Heres the template:

<label>Show Completed: <input type="checkbox" class="pletedEnquiries" /></label>

I've no idea what I'm doing wrong here?

Edit

Here is a Jsfiddle of the problem: /

I'm trying to hook up a checkbox to my View, but as soon as I tick it, it stays checked, even when I click it again?

Here's part of the View:

views.PaginatedView = Backbone.View.extend({
    events: {
        'click input.pletedEnquiries': 'filterCompletedEnquiries'
    },
    filterCompletedEnquiries: function (e) {
        return e.currentTarget.checked;
    }
});

Heres the template:

<label>Show Completed: <input type="checkbox" class="pletedEnquiries" /></label>

I've no idea what I'm doing wrong here?

Edit

Here is a Jsfiddle of the problem: http://jsfiddle/9cvVv/167/

Share Improve this question edited Aug 30, 2012 at 11:57 Derick Bailey 72.9k23 gold badges212 silver badges221 bronze badges asked Aug 30, 2012 at 11:14 CallumVassCallumVass 11.5k27 gold badges88 silver badges155 bronze badges 2
  • Something else is happening due everything looks right for me. Can you reproduce the issue in a jsFiddle? – fguillen Commented Aug 30, 2012 at 11:20
  • jsfiddle/9cvVv/167 – CallumVass Commented Aug 30, 2012 at 11:40
Add a ment  | 

1 Answer 1

Reset to default 6

The problem is returning e.currentTarget.checked from your event handler. Returning true or false from this handler will check or uncheck the box for you


 filterCompletedEnquiries: function(e) {
        //return e.currentTarget.checked;
    },

ment out that return statement, and it works fine. You can still grab the info, but don't return anything from the method.


 filterCompletedEnquiries: function(e) {
        var isChecked = e.currentTarget.checked;
        // do stuff here, based on it being checked or not
    },

Edit

Here's an example, based on the conversation in the ments:


views.PaginatedView = Backbone.View.extend({
    events: {
        'click input.pletedEnquiries': 'pletedEnquiriesClicked'
    },

    // this is explicitly an event handler, and that's all it should be used for
    pletedEnquiriesClicked: function(e){
      this.showCompletedEnquiries = e.currentTarget.checked;
    },

    doSomething Else: function (e) {
        // now that we need to know, we can just check that attribute
        if (this.showCompletedEnquiries){
          // do something here
        }
    }
});

This is just one of many options you have, for making this work the way you want.

发布评论

评论列表(0)

  1. 暂无评论