【问题标题】:Set input integer value in reference to other integer input value to performe a search参考其他整数输入值设置输入整数值以执行搜索
【发布时间】:2020-05-26 13:26:54
【问题描述】:

我正在尝试创建一个搜索过滤器,允许我搜索商店中的商品。所以我有 2 个输入,我希望能够执行以下操作。

  1. 如果我的最低价格为空,则返回 0 作为值以执行搜索
  2. 如果最高价低于最低价,则将输入的最高价更改为最低价+1进行搜索
  3. 如果最高价格为空,则返回无穷大(或像 1000000 这样的非常大的数字)以执行搜索

我尝试在此之后创建脚本,但没有成功。所以我不确定我做错了什么。

我已经在 @Craicerjack 的帮助下更新了脚本,但仍然无法正常工作。

这是带有脚本的 html。

非常感谢您花时间帮助我!

<form method="get" action="https://magiccarpets.online/shop/" target="_self">
  <div class="custom_range_widget">
    <input type="number" name="min_price" min="0" max="50000" placeholder="Min." contenteditable="true" id="minprice" />
    <input type="number" name="max_price" min="0" max="50000" placeholder="Max." contenteditable="true" id="maxprice" />

    <script>
      function  PriceRange() {
        var max = document.getElementById("maxprice"),
        min = document.getElementById("minprice");

        if (min == "") {
          (int)document.getElementById("minprice").value = 0;
        }

        if (parseInt(max) <= parseInt(min){
          (int)document.getElementById("maxprice").value = ((int)document.getElementById("minprice").innerHTML + (int)1);
        }

        if (max == "") {
          (int)document.getElementById("maxprice").value = 1000000;
        }
      }
    </script>
  </div>
  <button type="submit" class="button">Filter</button>
</form>

【问题讨论】:

  • 几件事。 ((int)mminprice == "") - 如果你想转换为 int,你可以使用 parseInt(),然后像 parseInt(mminprice) 这样使用它。其次,您似乎正在尝试解析为 int,然后与 "" 进行比较。您还在函数顶部将您的 2 个变量命名为相同,这样您的 minprice input var 将覆盖您的 maxprice input var。然后你开始使用你还没有定义的变量mminpricemaxprice
  • 好的,所以,看看我是否理解而不是使用(int)var,我应该使用parseInt(var)
  • 是的,这是你应该做的一件事。
  • 我不明白你所说的“使用你尚未定义的变量”。 mminprice 我在这里打错了。但是为什么minprice 会覆盖maxprice?我怎样才能做到正确?
  • 您的代码中有所有这些随机的(int) 位。为什么?你觉得他们做什么?您甚至在哪里调用 PriceRange 函数?为什么你的 JavaScript 在表单中?

标签: javascript html forms function if-statement


【解决方案1】:

我认为,您应该在第二个if 中将innerHTML 替换为value

function PriceRange() {
    var max = document.getElementById("maxprice").value,
        min = document.getElementById("minprice").value;

    if (min == "") {
      document.getElementById("minprice").value = 0;
    }

    if (parseInt(max) <= parseInt(min)) {
        document.getElementById("maxprice").value = Number(document.getElementById("minprice").value) + 1;
    }

    if (max == "") {
      document.getElementById("maxprice").value = 1000000;
    }
}

【讨论】:

  • 嗨!我有这个 sn-p,我今天已经尝试过了,但我无法让它工作,即使调试器说没有 sintax 错误,从技术上讲它应该工作得很好。我不知道可能缺少什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
  • 2019-05-07
  • 1970-01-01
相关资源
最近更新 更多