Javascript 最小字符数文本框
我的代码因某种原因无法正常工作。以下是: html:Javascript 最小字符数文本框,javascript,html,textinput,Javascript,Html,Textinput,我的代码因某种原因无法正常工作。以下是: html: 对于javascript: var inpt = document.getElementsByName("post")[0]; // var inputValue=document.getElementById(post).value; if (inpt.value < 10) { return false; alert("Post must be longer than 10 characters."); } el
对于javascript:
var inpt = document.getElementsByName("post")[0];
// var inputValue=document.getElementById(post).value;
if (inpt.value < 10) {
return false;
alert("Post must be longer than 10 characters.");
} else {
return true;
}
var inpt=document.getElementsByName(“post”)[0];
//var inputValue=document.getElementById(post).value;
如果(输入值<10){
返回false;
警告(“帖子长度必须超过10个字符”);
}否则{
返回true;
}
我试着引用了第二行,也没有引用第二行,但都没用。另外,当我取消第二行的引号时,我确保将inpt.value更改为inputValue.length。将警报放在return语句之前。有两个问题
var inpt = document.getElementsByName("post")[0];
//need to test the length
if (inpt.value.length < 10) {
alert("Post must be longer than 10 characters.");
//return after the alert
return false;
} else {
return true;
}
var inpt=document.getElementsByName(“post”)[0];
//需要测试长度
如果(输入值长度<10){
警告(“帖子长度必须超过10个字符”);
//警报后返回
返回false;
}否则{
返回true;
}
还要确保脚本在事件中被触发
函数验证(){
var inpt=document.getElementsByName(“post”)[0];
//需要测试长度
如果(输入值长度<10){
警告(“帖子长度必须超过10个字符”);
//警报后返回
返回false;
}否则{
返回true;
}
}
拯救
应该是:inpt.value.length<10
@PHPglue仍然不起作用,因为您的代码可能是在name='post'
元素之前定义的。使用onload
克服此问题。您应该使用外部JavaScript引用
中脚本标记的src
属性,在代码上使用onload事件,如:var pre=onload;onload=function(){if(pre)pre();/*在此处运行代码*/}
。
var inpt = document.getElementsByName("post")[0];
//need to test the length
if (inpt.value.length < 10) {
alert("Post must be longer than 10 characters.");
//return after the alert
return false;
} else {
return true;
}