【发布时间】:2018-04-10 17:36:10
【问题描述】:
在 type="number" 的输入元素上侦听“点击”事件,如果 e.currentTarget.value 大于某个值,我想阻止输入增加。
我使用了e.preventDefault(),但是输入继续增加价值。我也试过e.stopImmediatePropagation(),输入继续增加值。我还检查了e.cancelable 的值,这是真的。同样在记录事件时,属性isDefaultPrevented 和isPropagationStopped 都为真。
我尝试的另一个解决方案是将输入的值设置回旧值,但随后一瞬间它发生了变化,这是不可取的。
有人知道为什么会这样吗?
// @method setFocus sets focus on input when clicked. Needed as FF won't focus if quantity is updated from spinners
// @return {Void}
, setFocus: function setFocus (e)
{
var value = parseInt(e.currentTarget.value, 10)
, qty_avail = this.model.getStockInfo().stock
, $input_quantity = this.$('[name="quantity"]');
if(value > qty_avail)
{
e.preventDefault();
this.$('.product-details-quantity-alert').html("There is only " + qty_avail + " piece(s) available with these options");
this.$('.product-details-quantity-alert').css("display", "initial");
}
else
{
this.$('.product-details-quantity-alert').css("display", "none");
$input_quantity.focus();
}
console.log(qty_avail);
console.log(value);
}
【问题讨论】:
-
你能解释一下如果
e.currentTarget.value大于某个值我想阻止输入增加。你的意思是阻止用户无法增加它,还是输入以某种方式自动增加?
标签: javascript jquery input preventdefault