选择标签,在某个最大值上设置选项



我试图添加一个选项到我的选择元素,以便能够选择超过一定值的最大选项。在这个例子中是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>
);
}

从问题中我可以理解为您想要显示">250"当用户选择选项4时。如果是这样,setMaxPrice(>250)将不能工作,因为>250不属于任何数据类型。你可以把它作为像setMaxPrice(">250")这样的字符串传递下去,这应该可以工作。

最新更新