<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
window.onload=function(){
var oIpt1=document.getElementById('ipt1');
var oBtn1=document.getElementById('btn1');
oBtn1.onclick=function(){
var oValue=oIpt1.oValue;
if(oValue.length>10){
return false;
}else{
return true;
}
}
}
</script>
</head>
<body>
<form>
<input type="text" id="ipt1">
<button type="submit" id="btn1">提交</button>
</form>
</body>
</html>
//你写的js是在页面加载的时候运行的。你想要实现的需要在页面加载完之后运行
好吧,放在onload里还是可以的
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form>
<input type="text" id="ipt1">
<button type="submit" id="btn1">提交</button>
</form>
<script type="text/javascript">
var oBtn1=document.getElementById('btn1');
oBtn1.onclick=function(e){
var oIpt1=document.getElementById('ipt1');
var oValue=oIpt1.value; //你写的是var oValue=oIpt1.oValue;
if(oValue.length>10){
e.preventDefault();
}else{
console.log(true);
}
}
</script>
</body>
</html>
oValue不是undefined吗
window.onload=function(){
var oIpt1=document.getElementById('ipt1');
var oBtn1=document.getElementById('btn1');
oBtn1.onclick=function(e){
var oValue=oIpt1.value;
if(oValue.length>10){
return false;
}else{
return true;
}
}
}
首先你用的是type为submit的按钮,所以会提交,其次,你的oValue.length>10时return的false没做处理啊