Assign the obtained text element to var x; why does it not take effect to determine whether the input text is empty? document.getElementById("demo").innerHTML; Is the element obtained a string or something else? It is a string. Why does using x.length to determine the length of a string not work?
<style>
input {border:1px solid #ddd;}
</style>
<p>
<form>
<label>Please Enter nickname:</label><input type="text" name="fname" id="demo"><button onclick="infoBtn()">Submit information</button>
</form>
<script>
function infoBtn(){
var x=document.getElementById("demo").innerHTML;
if(x.length == 0 ||x==""){
alert("输入不能为空");
}else{
alert("设置成功");
}
}
</script>
</p>
曾经蜡笔没有小新2017-05-18 11:02:49
First of all, I can't see whether your demo is input. If so, use val() instead of innerHTML. If not, you have to print out whether x can get the data.
PHPz2017-05-18 11:02:49
var x=document.getElementById("demo");
if(!x){
alert("输入不能为空");
} else {
alert("设置成功", x.innerHTML);
}