我想只得到正的值,有什么方法来防止它只使用html 请不要建议验证方法
当前回答
这个代码对我来说工作得很好。你能查一下吗:
<input type="number" name="test" min="0" oninput="validity.valid||(value='');">
其他回答
这个问题的答案是没有帮助的。因为它只在你使用上/下键时起作用,但如果你输入-11,它就不起作用。这是我用的一个小方法
这个是整数
$(".integer").live("keypress keyup", function (event) {
// console.log('int = '+$(this).val());
$(this).val($(this).val().replace(/[^\d].+/, ""));
if (event.which != 8 && (event.which < 48 || event.which > 57))
{
event.preventDefault();
}
});
当你有价格的时候
$(".numeric, .price").live("keypress keyup", function (event) {
// console.log('numeric = '+$(this).val());
$(this).val($(this).val().replace(/[^0-9\,\.]/g, ''));
if (event.which != 8 && (event.which != 44 || $(this).val().indexOf(',') != -1) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
简单的方法:
<input min='0' type=“number” onkeypress=“return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57”>
这个代码对我来说工作得很好。你能查一下吗:
<input type="number" name="test" min="0" oninput="validity.valid||(value='');">
简单地使用min="0"
< v-text-field v模型=“abc” 类= " ml-1 rounded-0” 概述了 密集的 label = "数量" type = "数量" min = " 0 " > < / v-text-field >
@Manwal的回答很好,但我喜欢少写几行代码的代码,这样可读性更好。另外,我喜欢在html中使用onclick/onkeypress的用法。
我建议的解决方案也是如此: 添加
min="0" onkeypress="return isNumberKey(event)"
到HTML输入和
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode > 31 && (charCode < 48 || charCode > 57));
}
作为一个javascript函数。
如前所述,它做的是一样的。这只是个人对如何解决问题的偏好。
推荐文章
- 使伸缩项目正确浮动
- 形式内联内的形式水平在twitter bootstrap?
- 自定义元素在HTML5中有效吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?
- 如何删除和清除所有的本地存储数据
- 强制打开“另存为…”弹出打开文本链接点击PDF在HTML
- 如何修改标签文本?
- 在HTML中还有其他有用的空格码吗,比如半空格的 , em-spaces, en-spaces等等?
- 输入触发器按钮单击