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>
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.
Why do we need to correct
input
用innerHTML
啊,用value
attributes?Use value and then add event listener