我正在尝试让一个文本框在用户输入数字 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/