gpt4 book ai didi

javascript - 使文本框只有5的倍数

转载 作者:行者123 更新时间:2023-11-30 13:14:13 24 4
gpt4 key购买 nike

我正在尝试让一个文本框在用户输入数字 1 时向其发送消息,例如。然后它应该显示一个弹出窗口,说明该产品仅提供 5 的倍数。

这是我的代码。

<script language="JavaScript">
function chk_boxquantity(inField) {
step3_submit_disable();

var fVal = inField.value;
var fBoxQuantity = 5;

if (mod(fVal, fBoxQuantity) != 5) {
alert('This product is only available in multiples of '+fBoxQuantity);
inField.value = Math.ceil(fVal/fBoxQuantity)*fBoxQuantity;
setTimeout(function() {
inField.focus();inField.select();gAutoBlur = false;
}, 10);
}

step3_submit_enable();
}
</script>

这是文本字段。

<input type="text" onkeypress="return handleEnter(this, event);" 
onblur="chk_boxquantity(this);" name="qty[<?php echo $card['id']; ?>]"
size="3" />

因此,无论用户点击什么地方,它都会弹出消息。但这行不通,有什么想法为什么不行吗?

谢谢,

乔纳

最佳答案

将您的代码从 mod() 更改为 % operator DEMO

来自

 if (mod(fVal, fBoxQuantity) != 5)

  if ((fVal % fBoxQuantity) != 0) 

完整的功能可能看起来像

function chk_boxquantity(inField)
{
var fVal = inField.value;
var fBoxQuantity = 5;

if ((fVal % fBoxQuantity) !== 0)
{
alert('This product is only available in multiples of '+fBoxQuantity);
inField.value = Math.ceil(fVal/fBoxQuantity)*fBoxQuantity ;
}

}

仅显示必需的代码。

关于javascript - 使文本框只有5的倍数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12723090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com