如何检查输入的数字是否在多个数字之间Jquery



我有一个输入,要求您输入的价格

<input type="number" class="purchase_price">

现在我需要Jquery做的是更改我的隐藏输入purchase_price_date 的值

<input type="hidden" name="purchase_price_data" class="purchase_price_data" value="">

然而,我需要它来检查用户在purchas_price输入框中输入的数字,并查看它在列表中的位置:如果该物品是为以下目的购买的:;

250-500将purchase_ price_;覆盖高达500〃;

501-751将purchase_ price_;覆盖高达750〃;

751-1000将purchase_ price_;覆盖高达1000〃;

我在尝试类似的东西

$('input.purchase_price').focus(function () {
var $this = $(this)
t = setInterval(
function () {
if (($this.val() < 250 || $this.val() > 1000) && $this.val().length != 0) {
if ($this.val() < 250) {
$("input.purchase_price_data").val("hello");
}

if ($this.val() > 250) {
$("input.purchase_price_data").val("hello something else");
}
if ($this.val() > 500) {
$("input.purchase_price_data").val("hello again");
}
}
}, 50)
})

但它正按照我需要的方式发挥作用,当我添加第三个if时,它就完全错了。(我在测试时将purchase_price_data输入为未隐藏(

有人能帮忙吗?

一个选项是对所有值使用<,使用if-else-if-则范围的下限将已由上一个if处理。

如果上限为500 ,则可以使用<=,例如val<=500

$('input.purchase_price').on("input", function() {
var val = $(this).val() * 1;
if (val <= 0)
$("input.purchase_price_data").val("please enter a value");
else if (val < 250) {
//0-249
$("input.purchase_price_data").val("cheapskate");
} else if (val < 500) {
//250-499
$("input.purchase_price_data").val("about right");
} else {
//500+
$("input.purchase_price_data").val("ooh, generous");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="purchase_price" />
<input class="purchase_price_data" />

另一种选择是使用一个具有范围和响应的数组,这将使其数据驱动,因此更灵活。

最新更新