簡體   English   中英

如果和其他陳述不起作用(javascript)(html)

[英]if and else statment not working (javascript) (html)

Javascript位:

 function emptyF(){
        var x = document.getElementById("get");
        if(x.value.match("")){
            alert("missing fields");
            return false;
        }
        else{
            return true;
        }
    }

html位:

<h1>Login</h1>
<form action="home.php" method="post">
Username: <input type="text" id="get" onkeyup="myFunction()" name="username" value="<?php echo $_COOKIE['name'];?>" /><br /><br />
<input type="submit" onclick="emptyF()" name="submit"  value="Log In" />

當我在文本框中鍵入內容並單擊“提交”時,我會收到警報,該警報只會在沒有輸入任何內容的情況下出現,但這會使警報發出,無論是否有東西。

如果要將值與空字符串進行比較,則直接將其比較:

if (x.value === "")

.match()函數會將其參數強制轉換為正則表達式,並且每個字符串都將匹配空字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM