如果输入字母的长度等于0但不起作用,我希望弹出警报
<div class="rame">
<input type=text placeholder="Name" style="position: relative; right:-750px" id="inp1">
<input type=text placeholder="Name2" style="position: relative; right:-250px" id="inp2">
<button onclick="check()">submit</button>
</div>
<script>
function check() {
var a = document.getElementById("inp1");
var b = document.getElementById("inp2");
if (a.length = 0) {
alert("Hello World")
}
}
</script>
答案 0 :(得分:4)
您应该致电if(a.value.length==0)
。请注意=
和==
之间的区别。
答案 1 :(得分:2)
我认为您尝试过此操作
<div class="rame">
<input type=text placeholder="Name" style="position: relative; right:-750px" id="inp1">
<input type=text placeholder="Name2" style="position: relative; right:-250px" id="inp2">
<button onclick="check()">submit</button>
</div>
<script>
function check() {
var a = document.getElementById("inp1");
var b = document.getElementById("inp2");
if (a.value.length == 0) {
alert("Hello World")
}
}
</script>