表格具有2个整数输入,其中一个始终必须较低,另一个始终必须较高。

huangapple go评论51阅读模式
英文:

form with 2 int inputs where one always has to be lower and one always has to higher

问题

我认为我的代码中很清楚我试图做什么。基本上,我试图使用输入的最大和最小参数,以使它们永远不会相互交叉。当然,这并不起作用,我正在使用React,并使用useState在表单提交时设置这些变量的值,并将这些变量传递到我的数据库获取操作中。我觉得使用两个状态,一个用于输入的临时值,一个用于将提交的值传递给获取操作,不是解决这个问题的好方法。

const [LowestPrice, setLowestPrice] = useState(0)
const [HighestPrice, setHighestPrice] = useState(500)

useEffect(() => {
  const getProps = async () => {
    const { data, count, error } = await backbase.from('products_2')
      .select('*', { count: 'exact' })
      .gte('price', LowestPrice)
      .lt('price', HighestPrice)
      .range(indexOfFirstItem, indexOfLastItem - 1)
  }
}, [LowestPrice, HighestPrice])

const handleSubmit = (e) => {
  e.preventDefault()

  setLowestPrice(document.getElementById("lowest_price")?.value)
  setHighestPrice(document.getElementById("highest_price")?.value)
}

<form onSubmit={handleSubmit}>
  <label htmlFor="lowest_price">minimum price</label>
  <input
    type="number"
    id="lowest_price"
    defaultValue={LowestPrice}
    min={0}
    max={document.getElementById("highest_price")?.value}
  />
  <label htmlFor="highest_price">maximum price</label>
  <input
    type="number"
    id="highest_price"
    defaultValue={HighestPrice}
    min={document.getElementById("lowest_price")?.value}
    max={500}
  />
  <button type="submit">apply filters</button>
</form>

我删除了代码中不必要的部分,以便更容易阅读。表单中的min和max是最相关的部分。

英文:

I think its's pretty clear in my code what I am trying to do. basically I'm trying to use the max and min parameter from the input to make it so that they can never cross each other. This doesn't work of course, I am using react and am using usestate to set the values whenever the form is submitted and pass these variables into my database fetch. I feel like using 2 states one for the temporary value of the input and one to pass the submitted value to the fetch is not a good way of solving this.

const [ LowestPrice, setLowestPrice ] = useState(0)
const [ HighestPrice, setHighestPrice ] = useState(500)

useEffect(() =&gt;{
const getProps = async () =&gt; {
const { data, count, error } = await backbase.from(&#39;products_2&#39;)
      .select(&#39;*&#39;, { count: &#39;exact&#39; })
      .gte(&#39;price&#39;, LowestPrice)
      .lt(&#39;price&#39;, HighestPrice)
      .range(indexOfFirstItem, indexOfLastItem - 1)
}}, [LowestPrice, HighestPrice])

const handleSubmit = (e) =&gt; {
    e.preventDefault()

    setLowestPrice(document.getElementById(&quot;lowest_price&quot;)?.value)
    setHighestPrice(document.getElementById(&quot;highest_price&quot;)?.value)
  }

&lt;form onSubmit={handleSubmit}&gt;
        &lt;label htmlFor=&quot;lowest_price&quot;&gt;minimum price&lt;/label&gt;
        &lt;input
          type=&quot;number&quot;
          id=&quot;lowest_price&quot;
          defaultValue={LowestPrice}
          min={0}
          max={document.getElementById(&quot;highest_price&quot;)?.value}
        /&gt;
        &lt;label htmlFor=&quot;highest_price&quot;&gt;maximum price&lt;/label&gt;
        &lt;input
          type=&quot;number&quot;
          id=&quot;highest_price&quot;
          defaultValue={HighestPrice}
          min={document.getElementById(&quot;lowest_price&quot;)?.value}
          max={500}
        /&gt;
        &lt;button type=&quot;submit&quot;&gt;apply filters&lt;/button&gt;
      &lt;/form&gt;

I left out unessential parts of the code to make it easier to read. It's the min and max in the form that are the most relevant.

答案1

得分: 1

首先,不要使用getElementById来监听变化。当组件的值发生变化时,React会触发重新渲染。为了保留值,我们使用useState。其次,您可以使用OnChange来在决定是否丢弃新值之前在状态中比较两个值。尝试像这样做:

const [lowestPrice, setLowestPrice] = useState(0);
const [highestPrice, setHighestPrice] = useState(1);

return (
  <form onSubmit={handleSubmit}>
    <label htmlFor="lowest_price">最低价格</label>
    <input
      onChange={e => e.target.value <= highestPrice && setLowestPrice(e.target.value)}
      value={lowestPrice}
      type="number"
      id="lowest_price"
      defaultValue={lowestPrice}
      min={0}
      max={highestPrice}
    />
    <label htmlFor="highest_price">最高价格</label>
    <input
      onChange={e => e.target.value > lowestPrice && setHighestPrice(e.target.value)}
      type="number"
      id="highest_price"
      defaultValue={highestPrice}
      value={highestPrice}
      min={lowestPrice}
      max={500}
    />
    <button type="submit">应用筛选</button>
  </form>
)

请注意,我已经将HTML实体字符进行了恢复,以使翻译的内容在代码中保持完整。

英文:

Firstly, don't use getElementById to listen to changes. React triggers a re-render on components when their value changes. In order to retain the value, we use useState. Secondly, you can use OnChange to compare between both values in state before deciding whether or not to discard the new value. Try something like this;

const [lowestPrice, setLowestPrice] = useState(0);
const [highestPrice, setHighestPrice] = useState(1);

return (
&lt;form onSubmit={handleSubmit}&gt;
    &lt;label htmlFor=&quot;lowest_price&quot;&gt;minimum price&lt;/label&gt;
    &lt;input
      onChange={e =&gt; e.target.value &lt;= highestPrice &amp;&amp; setLowestPrice(e.target.value)} value={lowestPrice}
      type=&quot;number&quot;
      id=&quot;lowest_price&quot;
      defaultValue={lowestPrice}
      value={lowestPrice}
      min={0}
      max={highestPrice}
    /&gt;
    &lt;label htmlFor=&quot;highest_price&quot;&gt;maximum price&lt;/label&gt;
    &lt;input
      onChange={e =&gt; e.target.value &gt; lowestPrice &amp;&amp; setHighestPrice(e.target.value)}
      type=&quot;number&quot;
      id=&quot;highest_price&quot;
      defaultValue={highestPrice}
      value={highestPrice}
      min={lowestPrice}
      max={500}
    /&gt;
    &lt;button type=&quot;submit&quot;&gt;apply filters&lt;/button&gt;
  &lt;/form&gt;
)

huangapple
  • 本文由 发表于 2023年6月1日 21:35:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定