Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将表单中的变量传递给React.js中的onclick函数?_Javascript_Reactjs - Fatal编程技术网

Javascript 如何将表单中的变量传递给React.js中的onclick函数?

Javascript 如何将表单中的变量传递给React.js中的onclick函数?,javascript,reactjs,Javascript,Reactjs,我试图实现的是在选择百分比后设置输入框的值。但要计算这个值,我需要购买价格的值。我尝试在onDownPaymentPercentChange函数中访问purchasePrice属性,但没有成功。我如何做到这一点 export default class PurchaseInfo extends Component{ constructor(props){ super(props); this.state ={ purchasePrice:'', d

我试图实现的是在选择百分比后设置输入框的值。但要计算这个值,我需要购买价格的值。我尝试在onDownPaymentPercentChange函数中访问purchasePrice属性,但没有成功。我如何做到这一点

export default class PurchaseInfo extends Component{
constructor(props){
    super(props);
    this.state ={
        purchasePrice:'',
        downPayment:'',
    }
}

onPurchasePriceChange = (e) => {
        this.setState({
            purchasePrice: e.target.value
        })
    }

onDownPaymentPercentChange = (e) => {
        console.log(e.target.value)
        this.setState({
            downPayment: e.target.value * this.purchasePrice
        })
    }
render(){
  return(
  <div>
     <label>Purchase Price</label>
     <br/>
     <input id='PurchasePrice' required placeholder='$' onChange={this.onPurchasePriceChange}></input>
     <br/>
      <br/>
     <label>Percentage of purchase price</label>      
     <br/>
     <select onChange={this.onDownPaymentPercentChange}>
                    <option value='.035'>3.5%</option>
                    <option value='.05'>.5%</option>
     </select>
     <br/>
     <br>
    <label> Down Payment <label />
      <br/>
     <input required placeholder='$' value={this.state.downPayment}> 
     </input>
 </div>
导出默认类PurchaseInfo扩展组件{
建造师(道具){
超级(道具);
这个州={
购买价格:'',
首付:'',
}
}
onPurchasePriceChange=(e)=>{
这是我的国家({
购买价格:e.target.value
})
}
onDownPaymentPercentChange=(e)=>{
console.log(例如target.value)
这是我的国家({
首期付款:e.target.value*this.purchasePrice
})
}
render(){
返回(
购买价格



购买价格的百分比
3.5% .5%

首付款

您试图访问名为
purchasePrice
的类属性,而不是状态值

onDownPaymentPercentChange=(e)=>{
这是我的国家({
首付:e.target.value*this.purchasePrice//问题在这里
})
}

通过如下状态对象访问
purchasePrice
this.state.purchasePrice
您试图访问名为
purchasePrice
的类属性,而不是状态值

onDownPaymentPercentChange=(e)=>{
这是我的国家({
首付:e.target.value*this.purchasePrice//问题在这里
})
}

通过如下状态对象访问
purchasePrice
this.state.purchasePrice
您可能无法正确访问您的状态。另外,请事先检查purchasedPrice状态

onDownPaymentPercentChange = (e) => {
   console.log(e.target.value);
   if(this.state.purchasePrice === ''){
      return;
   }
   this.setState({
        downPayment: e.target.value * this.state.purchasePrice
   });
}

您可能无法正确访问您的状态。此外,请事先检查purchasedPrice状态

onDownPaymentPercentChange = (e) => {
   console.log(e.target.value);
   if(this.state.purchasePrice === ''){
      return;
   }
   this.setState({
        downPayment: e.target.value * this.state.purchasePrice
   });
}

您不需要绑定箭头函数。它总是简单的。谢谢!您不需要绑定箭头函数。它总是简单的。谢谢!