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

javascript - ReactJs Checkbox - cannot uncheck - Stack Overflow

programmeradmin4浏览0评论

I am trying to set the checked prop of a checkbox using a state, updated code with handleChange function:

getInitialState: function() {
    return {
        selected:0,
        contactId: 0
    };
},

handleChange: function(e) {
    var id = this.state.contactId;

    console.log(e.target.checked);

    if (e.target.checked === true){

        console.log('selected');

        contactChannel.publish({
                    channel: "contact",
                    topic: "selectedContact",
                    data: {
                        id: [id]
                    }});

    } else{

        basketChannel.publish({
            channel: "basket",
            topic: "removePersonFromBasket",
            data: {
                id: [id]
            }
        });


        console.log('deselected flag');


    }
},
render: function() {

    var id = this.state.contactId;
    var isSelected = this.state.selected;

    console.log(isSelected);

    return (
        <div className="contact-selector">
            <input type="checkbox"
                   checked={isSelected}
                onChange={this.handleChange} />
        </div>
    );
}

However, once the checkbox is checked by default I cannot uncheck it, can anyone please tell me how to do this?

Many thanks

I am trying to set the checked prop of a checkbox using a state, updated code with handleChange function:

getInitialState: function() {
    return {
        selected:0,
        contactId: 0
    };
},

handleChange: function(e) {
    var id = this.state.contactId;

    console.log(e.target.checked);

    if (e.target.checked === true){

        console.log('selected');

        contactChannel.publish({
                    channel: "contact",
                    topic: "selectedContact",
                    data: {
                        id: [id]
                    }});

    } else{

        basketChannel.publish({
            channel: "basket",
            topic: "removePersonFromBasket",
            data: {
                id: [id]
            }
        });


        console.log('deselected flag');


    }
},
render: function() {

    var id = this.state.contactId;
    var isSelected = this.state.selected;

    console.log(isSelected);

    return (
        <div className="contact-selector">
            <input type="checkbox"
                   checked={isSelected}
                onChange={this.handleChange} />
        </div>
    );
}

However, once the checkbox is checked by default I cannot uncheck it, can anyone please tell me how to do this?

Many thanks

Share Improve this question edited Aug 6, 2015 at 8:13 Bomber asked Aug 6, 2015 at 7:57 BomberBomber 11k27 gold badges97 silver badges175 bronze badges 1
  • Can you add the handleChange function? – udidu Commented Aug 6, 2015 at 8:00
Add a ment  | 

3 Answers 3

Reset to default 5

In the handleChange function you are always using the same value for the state of the check box but you need to reverse it.

add this to your handleChange function:

handleChange:function(event){
    this.setState({selected: !this.state.selected}); //look at the !NOT sign

}

The problem is because you return with the wrong variable. Changing {isSelected} with {this.state.selected} should work.

getInitialState: function() {
    return {
        selected:0,
        contactId: 0
    };
},


render: function() {

    var id = this.state.contactId;
    var isSelected = this.state.selected;

    console.log(isSelected);

    return (
        <div className="contact-selector">
            <input type="checkbox"
                   checked={this.state.selected}
                onChange={this.handleChange} />
        </div>
    );
}
getInitialState: function() {
    return {
        selected:0,
        contactId: 0
    };
},

handleChange:function(event){
    this.setState({selected: !this.state.selected});
    isSelected = this.state.selected;

    console.log(isSelected);
}

render: function() {

    var id = this.state.contactId;
    var isSelected = this.state.selected;

    console.log(isSelected);

    return (
        <div className="contact-selector">
            <input type="checkbox"
                   checked={isSelected}
                onChange={this.handleChange} />
        </div>
    );
}
发布评论

评论列表(0)

  1. 暂无评论