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 use react ref to get value from html select element? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use react ref to get value from html select element? - Stack Overflow

programmeradmin2浏览0评论

I have a form in which I am using html select element.

  <select className="form-control" id="ntype" required >
      <option value = "">None</option>
      <option value = "1">1</option>
      <option value = "2">2</option>
      <option value = "3">3</option>
  </select>

I know with html input type element we can attach ref like below code

ref = {(input) => { this.nHeading = input; }}

and

<input
    type        = "text"
    className   = "form-control"
    id          = "tInput"
    placeholder = "Sample input"
    ref         = {(input) => { this.sInput = input; }}
    required
/>

How can I attach ref to <Select> element and get selected option value from the attached ref when form is submitted?

Do I need to attach ref to each options or select element itself?

I have a form in which I am using html select element.

  <select className="form-control" id="ntype" required >
      <option value = "">None</option>
      <option value = "1">1</option>
      <option value = "2">2</option>
      <option value = "3">3</option>
  </select>

I know with html input type element we can attach ref like below code

ref = {(input) => { this.nHeading = input; }}

and

<input
    type        = "text"
    className   = "form-control"
    id          = "tInput"
    placeholder = "Sample input"
    ref         = {(input) => { this.sInput = input; }}
    required
/>

How can I attach ref to <Select> element and get selected option value from the attached ref when form is submitted?

Do I need to attach ref to each options or select element itself?

Share Improve this question asked Apr 17, 2017 at 15:25 WitVaultWitVault 24.1k20 gold badges106 silver badges137 bronze badges 5
  • 1 I'm curious as to why you want to do this with refs, rather than have the <select> model some other piece of state and use that instead? – rossipedia Commented Apr 17, 2017 at 15:28
  • did you try adding it to the select itself and then just using regular javascript on the element? i would also second @rossipedia though if you don't have a good reason for doing it this way – aw04 Commented Apr 17, 2017 at 15:34
  • @rossipedia I don't want to use controlled element via states, I want to use refs instead. – WitVault Commented Apr 17, 2017 at 15:36
  • @aw04 I just don't want to store state with form elements, because Component already has other important states. It may not be good reason but still I want to do it in ref way :) – WitVault Commented Apr 17, 2017 at 15:41
  • sure, i think the bottom part of the answer by @Shubham Khatri is the literal answer to your question then – aw04 Commented Apr 17, 2017 at 15:45
Add a ment  | 

4 Answers 4

Reset to default 5

You can store the value in a state on change and later use that i.e make it a controlled ponent

class App extends React.Component {
constructor() {
super();
    this.state = {selectValue: ''}
    }
    callThis = (e) => {
        this.setState({selectValue: e.target.value}, ()=> {console.log(this.state.selectValue)});
        
    }
    render() {
    
      return (
          <select onChange={this.callThis}className="form-control" id="ntype" required >
              <option value = "">None</option>
              <option value = "1">1</option>
              <option value = "2">2</option>
              <option value = "3">3</option>
          </select>
         
      )
    }
}
ReactDOM.render(<App/>, document.getElementById('app'));
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>

Or you can use refs to get the value like this

class App extends React.Component {
constructor() {
super();
    this.state = {selectValue: ''}
    }
    callThis = (e) => {
        console.log(this.selectVal.value)
        
    }
    render() {
    
      return (
          <div><select ref={(input) => this.selectVal = input} className="form-control" id="ntype" required >
              <option value = "">None</option>
              <option value = "1">1</option>
              <option value = "2">2</option>
              <option value = "3">3</option>
          </select>
          <input type="button" value="click" onClick={this.callThis}/></div>
      )
    }
}
ReactDOM.render(<App/>, document.getElementById('app'));
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>

If you are using React functional ponents, you can do the following.

import { useState, useRef } from 'react';

...

  const selectRef = useRef(null)
  <select className="form-control" id="ntype" required ref={selectRef}>
      <option value = "">None</option>
      <option value = "1">1</option>
      <option value = "2">2</option>
      <option value = "3">3</option>
  </select>

then you can access the value in your submit logic by,

selectRef.current.value

I would give each element a value and onChange handler for the select element like this

<select className="form-control"onChange={this.onChange.bind(this)}>
    <option value="">None</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

And define an onChange handler that sets the state of the selection

onChange(e){
    this.setState({value: e.target.value}, function(){

    // do other stuff

});

}

You can just use an onChange function to set the value of the Ref. E.g.:

<select className="form-control" id="ntype" required onChange={e => {yourRef.current = e.target.value}} >
      <option value = "">None</option>
      <option value = "1">1</option>
      <option value = "2">2</option>
      <option value = "3">3</option>
  </select>
发布评论

评论列表(0)

  1. 暂无评论