React:获取自定义复选框的checked属性。

20

我想对复选框进行一些自定义,可以看起来像这样:

enter image description here

因此,我将我的自定义复选框包裹在一个React组件中:

require('../../less/ck-checkbox.less');
var React = require('react');

var CkCheckbox = React.createClass({
  propTypes: {
    name: React.PropTypes.string,
    text: React.PropTypes.string,
    defaultChecked: React.PropTypes.bool,
    onChange: React.PropTypes.func
  },
  getDefaultProps: function() {
    return {
      name: 'checkbox',
      text: '',
      defaultChecked: false,
      onChange: function () {}
    };
  },
  render: function() {
    return (
      <div className="ck-checkbox">
        <label>
          <input
            type="checkbox"
            name={this.props.name}
            ref="c"
            defaultChecked={this.props.defaultChecked}
            onChange={this.props.onChange.bind(this, this.refs.c.checked)}
          />
            <span className="icons">
              <span className="icon-checked-o icon-true"></span>
              <span className="icon-circle-o icon-false"></span>
            </span>
            {this.props.text.length > 0 ?
              <span className="ck-checkbox-text">{this.props.text}</span> : null
            }
          </label>
        </div>
      );
    }
});

module.exports = CkCheckbox;

我的容器长这样:

var React = require('react');

var CkCheckbox = require('./CkCheckbox.js');

var Test = React.createClass({
  render: function() {
    return (
      <div>
        <CkCheckbox onChange={this._handleChange}/>
      </div>
    );
  },

  _handleChange: function(checked, e) {
    console.log(checked)
  }
});

module.exports = Test;

您可以看到,我试图在onChange回调中绑定this.refs.c.checked,但它不起作用。

那么,在_Test_组件的_handleChange函数中,我怎样才能获取自定义复选框的checked状态呢?

2个回答

26
在这种情况下,您不需要使用refs,因为可以从e参数获取已检查的属性。
// CkCheckbox component
 <input type="checkbox" 
     name={this.props.name} 
     defaultChecked={this.props.defaultChecked} 
     onChange={ this.props.onChange } />

// Test component
_handleChange: function(e) {
  var checked = e.target.checked;
  console.log(checked)
}

示例

或者,如果你只想传递checked属性,可以像这样做。

// CkCheckbox component
handleChange: function (e) {
  this.props.onChange(e.target.checked);
},

<input type="checkbox" 
   name={this.props.name} 
   defaultChecked={this.props.defaultChecked} 
   onChange={ this.handleChange } />

// in Test component 
_handleChange: function(checked) {
  console.log(checked)
}

示例


0

这是一个简单的示例,您可以使用它来获取自定义值或选中框的值,并检查该框是否已选中。

export default class SearchBoxContainer extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      boxAll: false
    };
  }

  handleChange = event => {
    this.setState({ boxAll: event.target.checked }, () => {
      console.log("This returned true or false", this.state.boxAll);
    });
  };

  render() {
    return (
      <div className="search-container">
        <SearchBox />
        <div className="filter-country">
          <h1>Filter</h1>
          <div className="filter-country-container">
            <div>
              <input
                type="checkbox"
                id="checkBoxUS"
                name="US"
                value="US"
                onChange={this.handleChange}
              />
              <label htmlFor="US">All Jobs</label>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

在这个例子中,重要的是要知道我只使用“setState()”回调来进行演示,但你可以通过props或者其他方法将值传递给其他组件。此外,当复选框被选中时,值将返回“true”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接