2017-11-11 174 views
1

我正在開發一個簡單的民意調查,以此作爲我在reactjs上發展的方式。到目前爲止,我想出了以下幾點:Reactjs:獲取單選按鈕的價值

//App.js 
import React, { Component } from 'react'; 
import './App.css'; 

const PollOption = ({options}) => { 


return (
    <div className="pollOption"> 
     {options.map((choice, index) => (
     <label key={index}> 
     <input type="radio" 
       name="vote" 
       value={choice.value} 
       key={index} 
       defaultChecked={choice.value} 
       onChange={() => this.props.onChange()}/> 
       {choice.text} 
     </label> 
    ))} 
    </div> 
    ); 
}; 



class OpinionPoll extends Component{ 
    constructor(props) { 
    super(props); 
    this.state = {selectedOption: ''} 
    } 

    handleClick(){ 
    console.log('button clicked'); 
    } 

    handleOnChange(){ 
    console.log('foo'); 
    } 

    render(){ 
    return (
     <div className="poll"> 
     {this.props.model.question} 
     <PollOption 
      options={this.props.model.choices} 
      onChange={() => this.handleOnChange()}/> 

     <button onClick={() => this.handleClick()}>Vote!</button> 
     </div> 
    ); 
    } 
} 


    export default OpinionPoll; 

    //index.js 
    var json = { 
      question: 'Do you support cookies in cakes?', 
      choices: 
      [ 
       {text: "Yes", value: 1}, 
       {text: "No", value: 2} 
      ] 
     } 
    const root = document.getElementById("root"); 
    render(<OpinionPoll model ={json} />, root) 

我想要單擊按鈕時獲取單選按鈕的值。

回答

2

只需少許修改的@Shubham卡特里答案添加checked屬性,選擇狀態。演示在這裏:https://codesandbox.io/s/vqz25ov285

const json = { 
    question: 'Do you support cookies in cakes?', 
    choices: 
    [ 
    { text: 'Yes', value: '1' }, 
    { text: 'No', value: '2' } 
    ] 
} 

const PollOption = ({ options, selected, onChange }) => { 
    return (
    <div className="pollOption"> 
     {options.map((choice, index) => (
     <label key={index}> 
      <input type="radio" 
      name="vote" 
      value={choice.value} 
      key={index} 
      checked={selected === choice.value} 
      onChange={onChange} /> 
      {choice.text} 
     </label> 
    ))} 
    </div> 
); 
}; 

class OpinionPoll extends React.Component { 
    constructor(props) { 
    super(props); 
    this.state = { selectedOption: '' } 
    } 

    handleClick() { 
    console.log('submitted option', this.state.selectedOption); 
    } 

    handleOnChange(e) { 
    console.log('selected option', e.target.value); 
    this.setState({ selectedOption: e.target.value}); 
    } 

    render() { 
    return (
     <div className="poll"> 
     {this.props.model.question} 
     <PollOption 
      options={this.props.model.choices} 
      onChange={(e) => this.handleOnChange(e)} 
      selected={this.state.selectedOption} /> 
     <button onClick={() => this.handleClick()}>Vote!</button> 
     </div> 
    ); 
    } 
} 

render(<OpinionPoll model={json} />, document.getElementById('root')); 
+0

在localhost和jsfiddle上的相同代碼https://jsfiddle.net/69z2wepo/91274/當單選按鈕被選中時我可以在控制檯中看到狀態,但單選按鈕沒有被檢查,爲什麼? – jwesonga

+0

請注意,我將json選項值更改爲字符串。元素值將被轉換爲字符串。所以如果沒有這種改變,你的'checked = {selected === choice.value}'會比較''1'=== 1'這是錯誤的。你可以使用double equals(比如'selected == choice.value',但是更好的做法是'selected === choice.value.toString()' –

1

PollOption是一個功能組件,因此this關鍵字不是無法訪問它,所以onChange={() => this.props.onChange()}將無法​​正常工作。您還需要將選定的值傳遞給父代。

也作爲@RickyJolly在評論中提到您需要checked屬性onChange被觸發。

const PollOption = ({options, onChange, selectedValue}) => { 
    return (
    <div className="pollOption"> 
     {options.map((choice, index) => (
     <label key={index}> 
     <input type="radio" 
       name="vote" 
       value={choice.value} 
       key={index} 
       checked={selectedValue === choice.value} 
       onChange={(e) => onChange(e.target.value)}/> 
       {choice.text} 
     </label> 
    ))} 
    </div> 
    ); 
}; 

class OpinionPoll extends Component{ 
    constructor(props) { 
    super(props); 
    this.state = {selectedOption: ''} 
    } 

    handleClick(){ 
    console.log('button clicked'); 
    } 

    handleOnChange(val){ 
    console.log('foo', val); 
    } 

    render(){ 
    return (
     <div className="poll"> 
     {this.props.model.question} 
     <PollOption 
      options={this.props.model.choices} 
      onChange={(val) => this.handleOnChange(val)} 
      selectedValue={this.state.selectedOption} 
     /> 

     <button onClick={() => this.handleClick()}>Vote!</button> 
     </div> 
    ); 
    } 
} 
+0

很好的回答,但一個小問題是,'onChange'事件將不會觸發因爲收音機沒有選中屬性改變,因此它不會註冊的改變。 –

+0

謝謝之前沒有太多關注它。更新了答案 –