我不知道爲什麼函數不會調用。最初它來自onsubmit =「return checkFilled()」的表單,但我也嘗試用一個按鈕調用onclick事件,它永遠不會進入函數。下面是代碼:javascript函數不被調用
function checkFilled() {
alert("Entered"); //just to test if entering function
return false;
var currentBox = document.getElementById("livCost").value;
if (currentBox == null) {
alert("Not all preferences are filled out!");
return false;
}
currentBox = document.getElementById("travOps").value;
else if (currentBox == "Select") {
alert("Not all preferences are filled out!");
return false;
} else
return true;
}
<form onsubmit="return checkFilled()" method="post" action="recommendScript.php">
<p>Cheap living cost :
<select name="livingCost" id="livCost">
<option selected>Select</option>
<option value="1">1</option>
<option value="2">2</option>
<p>Travel Opportunities :
<select name="travelOps" id="travOps">
<option selected>Select</option>
<option value="1">1</option>
<option value="2">2</option>
</select></p>
<input type="submit" value="Submit" class="loginButton" />
爲什麼不能這樣運行?
在Javascript控制檯中是否有任何錯誤? – Barmar
@AlonEitan之前有一個alert()。 – Barmar
您是否錯過了您嘗試運行的代碼中的關閉腳本標記,或者僅僅是將代碼複製到您的問題的問題? – Calum