我正在編寫一個程序,讓用戶輸入一個電話號碼,並告訴用戶它是否被接受/拒絕基於幾個條件。 我已經完成了計算的功能,但是我無法讓JavaScript使用HTML文檔:找不到它。未在HTML文檔中定義JavaScript函數,爲什麼不呢?
這裏有兩個代碼:(JS然後HTML):
function testanummer(nummer){
nummer = document.getElementById("a").value;
if isNaN(nummer){
console.log("Hi! This phone number appears faulty, please try again.");
} else if (nummer.length <=8) && else if (nummer.length >=14) {
console.log("Thanks! We'll be in touch shortly.");
} else {
console.log("Hi! This phone number appears faulty, please try again.");
}
HTML:
<html>
<head>
<title> Uppgift nummer 5</title>
<script src="testanummer.js"></script>
</head>
<body>
<form>
<label for="a">Skriv in ditt telefonnummer här: </label>
<input type="text" name="nummer" id="a">
<input type = "button" value ="Testa telefonnummer" onclick="testanummer(nummer);">
</form>
</body>
</html>
任何想法,爲什麼它不工作?我得到這個錯誤:
Uncaught SyntaxError: Unexpected identifier
和:
Uncaught ReferenceError: testanummer is not defined
任何想法,爲什麼它不工作?
非常感謝!
你 「nummer」 在'onclick'事件變量是不確定的。將其更改爲'onclick =「testanummer(this.value)」'。或者從函數參數中刪除'nummer',並且只需調用'testanummer()',因爲無論如何您都可以在函數中檢索輸入的數字。 – Jules
該函數未定義,因爲您有語法錯誤。這個錯誤信息會告訴你在哪裏(你必須將所有if語句的條件包含在括號中,如:if(isNaN(nummer))')。 –
你的JS格式不正確,testanumer.js的確切內容是? – Nix