根据 jsx 中 select 标签的下拉列表更改反应状态变量
Changing a react state variable according to drop down of select tag in jsx
我有一个表格,其中可以选择 select 预订座位数。我还有一个状态变量 noofseats,默认情况下为 0,应根据 select 标记中的选项 selected 进行更改。我怎么做?我正在尝试 onchange,除了值仍然保持在 0
TicketConfirm.js
import React from 'react'
import { useParams } from 'react-router-dom'
import { useState,useEffect } from 'react';
import './Ticketconfirm.css'
export default function Ticketconfirm() {
const [noofseats, setnoofseats] = useState(0)
const [totalcost, settotalcost] = useState(0)
useEffect(() => {
settotalcost(noofseats*ticketprice)
}, [])
const { busid, busname, fromcity, tocity, ticketprice, seatsleft, starttime, reachtime } = useParams();
return (
<>
<br /><br />
<h1 style={{color:'white'}}>{noofseats}</h1>
<h1 className='text-center' style={{color:'white'}}>Booking Confirmation</h1>
<div className='text-center' style={{ width:'900px',border:'2px solid white',marginLeft:'300px',marginTop:'10px'}}>
<div className="parent" >
<h2 style={{fontSize:'25px',color: 'white',padding:'25px' }}>ID {busid}</h2>
<h2 style={{fontSize:'25px', color: 'white',padding:'25px' }}>From {fromcity}</h2>
<h2 style={{fontSize:'25px', color: 'white',padding:'25px' }}>To {tocity}</h2>
<h2 style={{fontSize:'25px', color: 'white',padding:'25px' }}>Bus {busname}</h2>
<h2 style={{fontSize:'25px', color: 'white',padding:'25px' }}>Starts {starttime}</h2>
<h2 style={{fontSize:'25px', color: 'white',padding:'25px' }}>Reaches {reachtime}</h2>
</div>
<div className="parent2">
<h2 style={{fontSize:'25px', color: 'white',padding:'25px' }}>{ticketprice} per seat</h2>
<h2 style={{fontSize:'25px', color: 'white',padding:'25px' }}>{seatsleft} seats left</h2>
{/* <input style={{width:'200px',height:'50px',marginLeft:'20px'}}></input> */}
<div>
<label style={{color:'white',fontSize:'25px'}} for="cars">Number of Tickets</label>
<select style={{width:'80px',height:'50px',marginLeft:'20px'}} name="cars" id="cars">
<option onChange={e=>setnoofseats(e.target.value)} value = "1">1</option>
<option onChange={e=>setnoofseats(e.target.value)} value="2">2</option>
<option onChange={e=>setnoofseats(e.target.value)} value="3">3</option>
<option onChange={e=>setnoofseats(e.target.value)} value="4">4</option>
<option onChange={e=>setnoofseats(e.target.value)} value="4">5</option>
<option onChange={e=>setnoofseats(e.target.value)} value="4">6</option>
</select>
</div>
<div style={{padding:'25px',paddingBottom:'50px'}}>
<button style={{boxShadow:'none'}} className="btn btn-success">Pay ₹{totalcost} </button>
</div>
</div>
</div>
</>
)
}
您已经在选项组件上使用了onChange,您必须在select上使用它。而且你还错过了 useEffect 依赖。
import React, { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import './Ticketconfirm.css';
export default function Ticketconfirm() {
const [noofseats, setnoofseats] = useState(0);
const [totalcost, settotalcost] = useState(0);
useEffect(() => {
settotalcost(noofseats * ticketprice);
}, [noofseats]);
const {
busid,
busname,
fromcity,
tocity,
ticketprice,
seatsleft,
starttime,
reachtime,
} = useParams();
return (
<>
<br />
<br />
<h1 style={{ color: 'white' }}>{noofseats}</h1>
<h1 className="text-center" style={{ color: 'white' }}>
Booking Confirmation
</h1>
<div
className="text-center"
style={{
width: '900px',
border: '2px solid white',
marginLeft: '300px',
marginTop: '10px',
}}
>
<div className="parent">
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
ID {busid}
</h2>
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
From {fromcity}
</h2>
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
To {tocity}
</h2>
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
Bus {busname}
</h2>
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
Starts {starttime}
</h2>
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
Reaches {reachtime}
</h2>
</div>
<div className="parent2">
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
{ticketprice} per seat
</h2>
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
{seatsleft} seats left
</h2>
{/* <input style={{width:'200px',height:'50px',marginLeft:'20px'}}></input> */}
<div>
<label style={{ color: 'white', fontSize: '25px' }} for="cars">
Number of Tickets
</label>
<select
style={{ width: '80px', height: '50px', marginLeft: '20px' }}
onChange={(e) => setnoofseats(e.target.value)}
name="cars"
id="cars"
>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="4">5</option>
<option value="4">6</option>
</select>
</div>
<div style={{ padding: '25px', paddingBottom: '50px' }}>
<button style={{ boxShadow: 'none' }} className="btn btn-success">
Pay ₹{totalcost}{' '}
</button>
</div>
</div>
</div>
</>
);
}
将您的 Select 标签 更改为:
<select value={noofseats} onChange={(e=> setnoofseats(e.target.value))} style={{width:'80px',height:'50px',marginLeft:'20px'}} name="cars" id="cars">
<option value = "1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
每次 noofseats
更改时,useEffect 都应该 运行。所以将其添加为依赖项:
useEffect(() => {
settotalcost(noofseats*ticketprice)
}, [noofseats])
我有一个表格,其中可以选择 select 预订座位数。我还有一个状态变量 noofseats,默认情况下为 0,应根据 select 标记中的选项 selected 进行更改。我怎么做?我正在尝试 onchange,除了值仍然保持在 0
TicketConfirm.js
import React from 'react'
import { useParams } from 'react-router-dom'
import { useState,useEffect } from 'react';
import './Ticketconfirm.css'
export default function Ticketconfirm() {
const [noofseats, setnoofseats] = useState(0)
const [totalcost, settotalcost] = useState(0)
useEffect(() => {
settotalcost(noofseats*ticketprice)
}, [])
const { busid, busname, fromcity, tocity, ticketprice, seatsleft, starttime, reachtime } = useParams();
return (
<>
<br /><br />
<h1 style={{color:'white'}}>{noofseats}</h1>
<h1 className='text-center' style={{color:'white'}}>Booking Confirmation</h1>
<div className='text-center' style={{ width:'900px',border:'2px solid white',marginLeft:'300px',marginTop:'10px'}}>
<div className="parent" >
<h2 style={{fontSize:'25px',color: 'white',padding:'25px' }}>ID {busid}</h2>
<h2 style={{fontSize:'25px', color: 'white',padding:'25px' }}>From {fromcity}</h2>
<h2 style={{fontSize:'25px', color: 'white',padding:'25px' }}>To {tocity}</h2>
<h2 style={{fontSize:'25px', color: 'white',padding:'25px' }}>Bus {busname}</h2>
<h2 style={{fontSize:'25px', color: 'white',padding:'25px' }}>Starts {starttime}</h2>
<h2 style={{fontSize:'25px', color: 'white',padding:'25px' }}>Reaches {reachtime}</h2>
</div>
<div className="parent2">
<h2 style={{fontSize:'25px', color: 'white',padding:'25px' }}>{ticketprice} per seat</h2>
<h2 style={{fontSize:'25px', color: 'white',padding:'25px' }}>{seatsleft} seats left</h2>
{/* <input style={{width:'200px',height:'50px',marginLeft:'20px'}}></input> */}
<div>
<label style={{color:'white',fontSize:'25px'}} for="cars">Number of Tickets</label>
<select style={{width:'80px',height:'50px',marginLeft:'20px'}} name="cars" id="cars">
<option onChange={e=>setnoofseats(e.target.value)} value = "1">1</option>
<option onChange={e=>setnoofseats(e.target.value)} value="2">2</option>
<option onChange={e=>setnoofseats(e.target.value)} value="3">3</option>
<option onChange={e=>setnoofseats(e.target.value)} value="4">4</option>
<option onChange={e=>setnoofseats(e.target.value)} value="4">5</option>
<option onChange={e=>setnoofseats(e.target.value)} value="4">6</option>
</select>
</div>
<div style={{padding:'25px',paddingBottom:'50px'}}>
<button style={{boxShadow:'none'}} className="btn btn-success">Pay ₹{totalcost} </button>
</div>
</div>
</div>
</>
)
}
您已经在选项组件上使用了onChange,您必须在select上使用它。而且你还错过了 useEffect 依赖。
import React, { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import './Ticketconfirm.css';
export default function Ticketconfirm() {
const [noofseats, setnoofseats] = useState(0);
const [totalcost, settotalcost] = useState(0);
useEffect(() => {
settotalcost(noofseats * ticketprice);
}, [noofseats]);
const {
busid,
busname,
fromcity,
tocity,
ticketprice,
seatsleft,
starttime,
reachtime,
} = useParams();
return (
<>
<br />
<br />
<h1 style={{ color: 'white' }}>{noofseats}</h1>
<h1 className="text-center" style={{ color: 'white' }}>
Booking Confirmation
</h1>
<div
className="text-center"
style={{
width: '900px',
border: '2px solid white',
marginLeft: '300px',
marginTop: '10px',
}}
>
<div className="parent">
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
ID {busid}
</h2>
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
From {fromcity}
</h2>
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
To {tocity}
</h2>
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
Bus {busname}
</h2>
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
Starts {starttime}
</h2>
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
Reaches {reachtime}
</h2>
</div>
<div className="parent2">
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
{ticketprice} per seat
</h2>
<h2 style={{ fontSize: '25px', color: 'white', padding: '25px' }}>
{seatsleft} seats left
</h2>
{/* <input style={{width:'200px',height:'50px',marginLeft:'20px'}}></input> */}
<div>
<label style={{ color: 'white', fontSize: '25px' }} for="cars">
Number of Tickets
</label>
<select
style={{ width: '80px', height: '50px', marginLeft: '20px' }}
onChange={(e) => setnoofseats(e.target.value)}
name="cars"
id="cars"
>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="4">5</option>
<option value="4">6</option>
</select>
</div>
<div style={{ padding: '25px', paddingBottom: '50px' }}>
<button style={{ boxShadow: 'none' }} className="btn btn-success">
Pay ₹{totalcost}{' '}
</button>
</div>
</div>
</div>
</>
);
}
将您的 Select 标签 更改为:
<select value={noofseats} onChange={(e=> setnoofseats(e.target.value))} style={{width:'80px',height:'50px',marginLeft:'20px'}} name="cars" id="cars">
<option value = "1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
每次 noofseats
更改时,useEffect 都应该 运行。所以将其添加为依赖项:
useEffect(() => {
settotalcost(noofseats*ticketprice)
}, [noofseats])