te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to return true or false using the input checkbox in react js? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to return true or false using the input checkbox in react js? - Stack Overflow

programmeradmin2浏览0评论

My input checkbox always returns true, when i mark it passsa true correctly, but when i unmark tb i get true. I would like the checkbox to return true when checked and false when unchecked, this way I would apply my logic in the handlesubmit function.


    handleChange = e => {
        const { name, value } = e.target;
        console.log(name, value);
        switch (name) {

          case 'period': this.json.schedule = 'period'; break;

    }


 <input
       type="checkbox"
       name="period"
       defaultValue
       onChange={this.handleChange}
 />

My input checkbox always returns true, when i mark it passsa true correctly, but when i unmark tb i get true. I would like the checkbox to return true when checked and false when unchecked, this way I would apply my logic in the handlesubmit function.


    handleChange = e => {
        const { name, value } = e.target;
        console.log(name, value);
        switch (name) {

          case 'period': this.json.schedule = 'period'; break;

    }


 <input
       type="checkbox"
       name="period"
       defaultValue
       onChange={this.handleChange}
 />

Share Improve this question edited Apr 16, 2019 at 19:59 Janio Carvalho asked Apr 16, 2019 at 19:55 Janio CarvalhoJanio Carvalho 1171 gold badge2 silver badges10 bronze badges 4
  • I added the function on change – Janio Carvalho Commented Apr 16, 2019 at 20:00
  • Your onChange callback function isnt returning anything, where are you checking for true and false? – Ryan Wilson Commented Apr 16, 2019 at 20:05
  • I think I understood the operation of the wrong ponent, believe that the simple fact of unmarking would return false, instead of true as I'm always receiving. – Janio Carvalho Commented Apr 16, 2019 at 20:07
  • add checked to const { name, value } = e.target – Neil Commented Apr 17, 2019 at 17:23
Add a ment  | 

4 Answers 4

Reset to default 10

Checkout the value of e.target.checked.

In your case, change this line: const { name, value } = e.target to include checked.

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      checked: true
    }
  }
  
  handleChange = (e) => {
    const { checked } = e.target
    this.setState({
      checked: checked
    })
  }
  
  render() {
    return (
      <div>
        <input type="checkbox"
               onChange={e => this.handleChange(e)}
               defaultChecked={this.state.checked}/>
        {this.state.checked.toString()}
      </div>
    )
  }
}

ReactDOM.render((<App/>), document.getElementById('testing'))
<script src="https://cdnjs.cloudflare./ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="testing"></div>

You can use useState.

import React, { useState } from 'react';

const App = () => {
    const [Check, setCheck] = useState(false);
    return (
        <div>
            <h1>{Check ? 'Checked' : 'Not checked'}</h1>
            <input type="checkbox" onChange={(e) => setCheck(e.target.checked) }/>
        </div>
    );
};

export default App;

You first need to define what check is considered? If it is check it is true and when it is not checked it is false. Here is some code to get you started.

{   this.state.data.map(function(item, index) { return (
    <input type="checkbox" checked={item.value} onChange={this.handleChange.bind(this, index)}/>
  );
      }.bind(this))
}

You aren't checking wether the box is checked or not, try:

handleChange = e => {
    if (document.getElementByClassName("period").checked) {
        // box is checked
    } else {
        // box is unchecked
    }
}
发布评论

评论列表(0)

  1. 暂无评论