Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数字输入字段不包括中间数字的范围_Javascript_Html_Input_Attributes - Fatal编程技术网

Javascript 数字输入字段不包括中间数字的范围

Javascript 数字输入字段不包括中间数字的范围,javascript,html,input,attributes,Javascript,Html,Input,Attributes,在输入字段中,如何排除数字范围 e、 g.允许用户输入值5,并且不允许-4到4之间的任何内容?。我没有要求max或min值 <input type="number" id="input" step="1" /> 谢谢首先将excmin和excmax属性添加到目标元素(在本例中为id=“input”的元素)的值中。 然后调用此函数并在页面加载时将元素传递给此函数: : 功能例外范围(元素) { var excmin=parseInt(element.getAttribute(“e

在输入字段中,如何排除数字范围

e、 g.允许用户输入
值<-5
值>5
,并且不允许-4到4之间的任何内容?。我没有要求
max
min

<input type="number" id="input" step="1" />


谢谢

首先将excmin和excmax属性添加到目标元素(在本例中为id=“input”的元素)的值中。
然后调用此函数并在页面加载时将元素传递给此函数:

:

功能例外范围(元素)
{
var excmin=parseInt(element.getAttribute(“excmin”))-1;
var excmax=parseInt(element.getAttribute(“excmax”)+1;
element.onchange=function()
{
if(元素值EXCMIN)
{
element.value=Math.round((parseInt(element.value)-excmin)/(excmin-excmax))*(excmin-excmax)+excmin;
}
};
}
现在,每当用户在这个范围内输入一个值时,当用户输入完数字后,它就会将其更改为接近允许的值
这就是脚本的样子:

function exceptionrange(element)
{
 var excmin=parseInt(element.getAttribute("excmin"))-1;
 var excmax=parseInt(element.getAttribute("excmax"))+1;
 element.onchange=function()
 {
  if(element.value<excmax&&element.value>excmin)
  {
   element.value=Math.round((parseInt(element.value)-excmin)/(excmin-excmax))*(excmin-excmax)+excmin;
  }
 };
}
window.addEventListener("load",function()
{
 var input=document.getElementById("input");
 exceptionrange(input);
});
功能例外范围(元素)
{
var excmin=parseInt(element.getAttribute(“excmin”))-1;
var excmax=parseInt(element.getAttribute(“excmax”)+1;
element.onchange=function()
{
if(元素值EXCMIN)
{
element.value=Math.round((parseInt(element.value)-excmin)/(excmin-excmax))*(excmin-excmax)+excmin;
}
};
}
addEventListener(“加载”,函数()
{
var输入=document.getElementById(“输入”);
例外范围(输入);
});
这是输入元素:

<input type="number" id="input" step="1" excmin="-4" excmax="4"/>


excmin
excmax
不是本机属性,对吗?对任何感兴趣的人都可以使用fiddle,它们不是本机属性,它们用于javascript函数exceptionrange
<input type="number" id="input" step="1" excmin="-4" excmax="4"/>