如何从react-bootstrap复选框获取值/属性?

Joh*_*yer 7 javascript checkbox reactjs react-bootstrap redux

我正在尝试使用react-bootstrap复选框(https://react-bootstrap.github.io/components.html#forms-controls),我需要在更改状态时触发事件.能够以编程方式取消/检查它和/或判断它是否被检查也是很好的.不幸的是,当代码被转换并呈现​​时,它将输入包装在div中.

我怎样才能在dom中找到它并操纵它?

我的代码看起来类似于:

import React, { PropTypes } from 'react';
import { Checkbox } from 'react-bootstrap';

const EditItem = (props) => {

  return (
    <div>
      <Checkbox style={{ marginLeft: '15px' }} >{props.itemLable}</Checkbox>
    </div>
  );
};

export default EditItem;
Run Code Online (Sandbox Code Playgroud)

浏览器渲染:

...
<div class="checkbox" style="margin-left: 15px;">
  <label>
    <input type="checkbox">
  </label>
</div>
...
Run Code Online (Sandbox Code Playgroud)

我在文档中看到了inputRef prop,但是我找不到任何这样的例子或让它自己工作.

Jor*_*ing 25

有两种方式:React方式和不那么React方式.

React的方法是通过传递道具并通过附加事件处理程序来响应其状态的变化来设置子组件的状态.在Checkbox的情况下,这意味着设置checkedonChange道具.

请注意下面的示例,父组件(App)如何跟踪Checkbox的状态,并且可以设置它this.setState并使用它进行查询this.state.checkboxChecked.

const { Checkbox, Button } = ReactBootstrap;

class App extends React.Component {
  constructor() {
    super();
    this.state = { checkboxChecked: false };
    this.handleChange = this.handleChange.bind(this);
    this.handleIsItChecked = this.handleIsItChecked.bind(this);
    this.handleToggle = this.handleToggle.bind(this);
  }
  
  render() {
    return (
      <div>
        <Checkbox
          checked={this.state.checkboxChecked}
          onChange={this.handleChange} />
        <Button type="button" onClick={this.handleToggle}>Toggle</Button>
        <Button type="button" onClick={this.handleIsItChecked}>Is it checked?</Button>
      </div>
    );
  }
  
  handleChange(evt) {
    this.setState({ checkboxChecked: evt.target.checked });
  }
  
  handleIsItChecked() {
    console.log(this.state.checkboxChecked ? 'Yes' : 'No');
  }
  
  handleToggle() {
    this.setState({ checkboxChecked: !this.state.checkboxChecked });
  }
}

ReactDOM.render(<App/>, document.querySelector('div'));
Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/react-bootstrap/0.30.8/react-bootstrap.min.js"></script>
<div></div>
Run Code Online (Sandbox Code Playgroud)

不那么React的方法是获取对呈现的DOM元素的引用并checked直接访问其属性.我不推荐这个,因为它必然会用icky强制性代码污染你可爱的功能性React代码.不过,使用React-Bootstrap可以通过设置inputRefprop来实现,如下例所示:

const { Checkbox, Button } = ReactBootstrap;

class App extends React.Component {
  constructor() {
    super();
    this.handleIsItChecked = this.handleIsItChecked.bind(this);
    this.handleToggle = this.handleToggle.bind(this);
  }
  
  render() {
    return (
      <div>
        <Checkbox
          onChange={this.handleChange}
          inputRef={ref => this.myCheckbox = ref} />
        <Button type="button" onClick={this.handleToggle}>Toggle</Button>
        <Button type="button" onClick={this.handleIsItChecked}>Is it checked?</Button>
      </div>
    );
  }
  
  handleIsItChecked() {
    console.log(this.myCheckbox.checked ? 'Yes' : 'No');
  }
  
  handleToggle() {
    this.myCheckbox.checked = !this.myCheckbox.checked;
  }
}

ReactDOM.render(<App/>, document.querySelector('div'));
Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/react-bootstrap/0.30.8/react-bootstrap.min.js"></script>
<div></div>
Run Code Online (Sandbox Code Playgroud)


小智 6

感谢以上的回答。当给定组件中有多个复选框时,我稍微概括了上述内容:

  constructor() {
    super();
    this.state = { YourInputName: false };
    this.handleCheckboxChange = this.handleCheckboxChange.bind(this);
  }

  render() {
    return (
      <div>
        <Checkbox
          name="YourInputName"
          onChange={this.handleCheckboxChange} />
      </div>
    );
  }

  handleCheckboxChange(event) {
    const target = event.target
    const checked = target.checked
    const name = target.name
    this.setState({
        [name]: checked,
    });
  }
Run Code Online (Sandbox Code Playgroud)