<input type=“number”>似乎不适用于大数字
<input type="number"> doesn't seem to work with large numbers
有这段代码:
<form id="filterprice" method=get onsubmit="window.location = 'catalog/{$category->url}{if $brand}/{$brand->url}{/if}?price_min='+price_min.value+'&price_max='+price_max.value;return false;">
<input type="number" id="price_min" name="price_min" min="0" onchange="check_min()" value={$price_min}> - <input type="number" id="price_max" name="price_max" min={$price_min} value={$price_max}>
<input type="submit" value="Подобрать">
</form>
<script>
function check_min() {
document.getElementById('price_max').min=document.getElementById('price_min').value;
if(document.getElementById('price_max').min > document.getElementById('price_max').value) {
document.getElementById('price_max').value=document.getElementById('price_max').min;
}
}
</script>
即我在"price_min"中有值 40000,在"price_max"中有值 40001000。每当我更改最后一个属性时,脚本都会将"price_max"的"min"属性设置为当前值"price_min"。因此,如果我将"price_min"更改为 40002,"price_max"的"min"属性设置为 40002。但它也将当前值"price_max"设置为 40002。似乎它只是"price_max"的 3 位数字。更不用说这些<input>
标签的"value"属性不会改变,即使我可以在页面上更改它们。所以问题是:<input type="number">
的价值有什么限制吗?为什么当涉及到大数字时它工作不正确?有什么解决办法吗?
问题是大于检查的是字符串而不是数字。所以你需要把它们转换成数字。
function check_min() {
document.getElementById('price_max').min = document.getElementById('price_min').value;
if (Number(document.getElementById('price_max').min) > Number(document.getElementById('price_max').value)) {
document.getElementById('price_max').value = document.getElementById('price_max').min;
}
}
<form id="filterprice" method=get>
<input type="number" id="price_min" name="price_min" min="0" onchange="check_min()" value="123">-
<input type="number" id="price_max" name="price_max" value="40001000">
<input type="submit" value="Подобрать">
</form>
看起来你从.min
和/或.value
得到的值是字符串,来自"40000"
和"40001000"
的词法比较使"40000"
变得更大。
只需使用if(parseInt(document.getElementById('price_max').min) > parseInt(document.getElementById('price_max').value).)
通过为"price_min"和"price_max"提供默认值,代码可以正常工作。
相关文章:
- html5 drawImage适用于firefox,而不是chrome
- Jquery Ajax POST不工作.适用于GET
- 如何调试遗留的javascript代码(适用于IE,但在Google Chrome中不正常)
- .load()适用于window,而不是ID
- 使用Jquery更改css样式适用于id's、 不在课堂上
- 提交按钮不会;不适用于Chrome或Firefox,但适用于Internet Explorer
- 为什么 javascript 中的“btoa”编码适用于 20 位字符串而不是 20 位整数
- Javascript String to Date适用于Chrome,而不是FF或Safari
- Onchange,向上,向下箭头键适用于FF和Chrome,但在IE中不起作用
- .load() 适用于 IE 和 dreamweaver 预览版,但不能使用 opera 和 chrome
- 找不到适用于 Highcharts 的 JSON 数组语法
- 为什么在jQuery中使用focusout适用于Firefox,而使用addEventListener则不适用;t
- jQuery选择菜单验证器适用于FF和Chrome,但不适用IE
- 单选按钮'onclick'适用于Chrome和Firefox,但不适用IE
- JQuery ScrollTop适用于chrome,但不适用firefox
- window.opener.location.href适用于IE,但不适用Chrome或Safari
- Frame Busting buster不完全适用于IE
- Jquery touch punch适用于Chrome和Firefox,但不适用IE
- 背景更改仅适用于chrome+$.预加载不适用;不起作用
- JavaScript window.location.replace适用于Firefox,但不适用IE或Chrome