【发布时间】:2021-10-22 07:11:47
【问题描述】:
我正在尝试向我的选择元素添加一个选项,以便能够选择超过某个值的最大选项。在本例中为 250。我尝试使用大于“>”比较运算符 >250,但它似乎不接受该语法。一个人怎么可能做到这一点?
import { useState } from "react";
import "./styles.css";
export default function App() {
const [minPrice, setMinPrice] = useState(0);
const [maxPrice, setMaxPrice] = useState(50);
const handlePrice = (value) => {
switch (value) {
case "1":
setMinPrice(0);
setMaxPrice(50);
break;
case "2":
setMinPrice(50);
setMaxPrice(100);
break;
case "3":
setMinPrice(100);
setMaxPrice(250);
break;
case "4":
setMinPrice(250);
setMaxPrice(>250); //doesn't work
break;
default:
setMinPrice(0);
setMaxPrice(50);
break;
}
};
return (
<div className="App">
<select
className="custom-select"
id="priceGroup"
onChange={(event) => handlePrice(event.target.value)}
>
<option value="1">Under $50</option>
<option value="2">$50 to $100</option>
<option value="3">$100 to $250</option>
<option value="4">Over $250</option>
</select>
<div>Your min price is {minPrice}</div>
<div>Your max price is {maxPrice}</div>
</div>
);
}
【问题讨论】:
-
在选择选项4的
<div>Your max price is {maxPrice}</div>/ span>时,您想要什么
标签: javascript reactjs react-hooks dropdown