input type="number" 用于在移动设备上获取数字键盘
Java 脚本
function numbersonly(myfield, e)
{
var key;
var keychar;
if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);
// control keys
if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
return true;
// numbers
else if ((("0123456789").indexOf(keychar) > -1))
return true;
// only one decimal point
else if ((keychar == "."))
{
if (myfield.value.indexOf(keychar) > -1)
return false;
}
else
return false;
}
html
<input type="number" onkeypress="return numbersonly(this, event)"/>