onkeyup="value=value.replace(/[^\d]/g,'')"? <input type="text" name="text" value="0" onkeyup="value=value.replace(/[^\d]/g,'')"/>怎样让他只能输入整数?例如:禁止输入0123456。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Untitled Document</title> <script type="text/javascript"> /** * author: develop_design_level * date: 2009-10-30 * @param {Object} id */ function $(id){ if (document.getElementById) { return document.getElementById(id); } else { return document.all.id; } } function checkInputText(){ var regExp = /^\d+$/g; var text = this.value; if(!regExp.test(text)){ this.value = '0'; } } window.onload = function(){ $('testId').onkeyup = checkInputText; }; </script> </head> <body> <form> <input type="text" id="testId" value="0"> </form> </body></html> 这样效果好<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head> <title>Untitled Page</title></head><body><script> function regInput(obj, reg, inputStr) { var docSel = document.selection.createRange() if (docSel.parentElement().tagName != "INPUT") return false oSel = docSel.duplicate() oSel.text = "" var srcRange = obj.createTextRange() oSel.setEndPoint("StartToStart", srcRange) var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length); return reg.test(str) }</script><input type="text" id="txt" onkeypress = "return regInput(this,/^\d*$/,String.fromCharCode(event.keyCode))"></body></html> 按照楼主的原意<input type="text" name="text" value="0" onkeyup="value=value.replace(/[^\d]/g,'').replace(/^0(\d+)$/,'$1')"/> 等等等 多维数组转json 页面执行顺序 如何在网页中设置闹钟功能!散分 Highcharts-2.2.3 pie 数字显示百分比BUG 为何程序执行不成功? js中数组元素(下标为关键字时)的访问 showbo版主大大求帮忙,js获取表单数据然后传递到php JS高手请进,如何做这样的切换效果? 秋水请进来拿分。 简单问题正解即结贴 收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 有关于做购物网站的jsp书吗?推荐几本
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script type="text/javascript">
/**
* author: develop_design_level
* date: 2009-10-30
* @param {Object} id
*/
function $(id){
if (document.getElementById) {
return document.getElementById(id);
}
else {
return document.all.id;
}
}
function checkInputText(){
var regExp = /^\d+$/g;
var text = this.value;
if(!regExp.test(text)){
this.value = '0';
}
}
window.onload = function(){
$('testId').onkeyup = checkInputText;
};
</script>
</head>
<body>
<form>
<input type="text" id="testId" value="0">
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
</head>
<body>
<script>
function regInput(obj, reg, inputStr) {
var docSel = document.selection.createRange()
if (docSel.parentElement().tagName != "INPUT") return false
oSel = docSel.duplicate()
oSel.text = ""
var srcRange = obj.createTextRange()
oSel.setEndPoint("StartToStart", srcRange)
var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length);
return reg.test(str)
}
</script>
<input type="text" id="txt" onkeypress = "return regInput(this,/^\d*$/,String.fromCharCode(event.keyCode))"></body>
</html>
<input type="text" name="text" value="0" onkeyup="value=value.replace(/[^\d]/g,'').replace(/^0(\d+)$/,'$1')"/>