Javascript 无法读取未定义的属性';最后';未定义的

Javascript 无法读取未定义的属性';最后';未定义的,javascript,Javascript,姓氏: 姓氏: 函数名称选择(){ if(为空(“+document.contest.last.value)){ document.contest.last.value=“姓氏” document.contest.last.focus(); document.contest.last.select(); } } 函数为空{ var len=s.长度; 对于(var i=0;i检查输入元素是否为空,您可以使用下面的代码而不是单独的函数 function nameselect(){ if(isBl

姓氏: 姓氏:

函数名称选择(){
if(为空(“+document.contest.last.value)){
document.contest.last.value=“姓氏”
document.contest.last.focus();
document.contest.last.select();
}
}
函数为空{
var len=s.长度;

对于(var i=0;i检查输入元素是否为空,您可以使用下面的代码而不是单独的函数

function nameselect(){
if(isBlank(" "+document.contest.last.value)){
    document.contest.last.value = "surname"
    document.contest.last.focus();
    document.contest.last.select();
}
}
function isBlank(s){
var len = s.length;
for(var i =0; i<len;++i){
if(s.charAt(i)!="") {return false;} 
}
return true; 
} 
//如果使用以下命令,请记住向输入元素添加id。例如
// 
if(document.getElementById(“myText”).value.trim().length==0){
//输入元素为空
}
还有一个占位符属性,当字段为空时,您可以使用它来显示特定文本

// If using the below remember to add id to the input element. Eg
// <input type="text" name="lname" id="lname" placeholder="Surname">
if (document.getElementById("myText").value.trim().length === 0) {
    // The input element is empty
}


last
是什么意思?@FastSnail,什么是
document.contest
?@raheel-almis是什么是
document.contest
?姓:@raheellamis,查看删除的答案。。。
// If using the below remember to add id to the input element. Eg
// <input type="text" name="lname" id="lname" placeholder="Surname">
if (document.getElementById("myText").value.trim().length === 0) {
    // The input element is empty
}
<input type="text" name="lname" placeholder="surname">