【问题标题】:Select tag, setting option over a certain max value选择标签,设置选项超过某个最大值
【发布时间】: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的&lt;div&gt;Your max price is {maxPrice}&lt;/div&gt; / span>时,您想要什么

标签: javascript reactjs react-hooks dropdown


【解决方案1】:

我可以从问题中了解到,当用户选择选项 4 时,您希望显示“>250”。 如果是这样,setMaxPrice(&gt;250) 将不起作用,因为&gt;250 它不属于任何数据类型。 您可以将其作为setMaxPrice("&gt;250") 之类的字符串传递下去,这样应该可以。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-24
    • 2021-03-25
    • 2016-09-26
    • 2017-02-21
    • 1970-01-01
    • 2018-06-22
    • 2020-03-13
    • 1970-01-01
    相关资源
    最近更新 更多