2017-03-08 148 views
0

我不知道爲什麼函數不會調用。最初它來自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" />

爲什麼不能這樣運行?

+0

在Javascript控制檯中是否有任何錯誤? – Barmar

+0

@AlonEitan之前有一個alert()。 – Barmar

+0

您是否錯過了您嘗試運行的代碼中的關閉腳本標記,或者僅僅是將代碼複製到您的問題的問題? – Calum

回答

-5

首先,改變<script><script type="text/javascript">,使其更安全,在你的js代碼的最後,請務必把結束腳本標籤</script>

+2

您是否嘗試過驗證這是否解決了問題? – mmgross

0

請務必關閉腳本標籤和表單標籤,如果仍然不工作打開JavaScript控制檯以查找任何語法錯誤

+0

您是否嘗試過驗證這是否解決了問題?其實你的答案的最後一部分是一個寶貴的建議,所以我刪除了我的downvote。 – mmgross

+0

不,我沒有但在提交時只會在表單標記關閉時觸發,否則沒有表單提交,我現在將測試您的代碼 –

+0

這裏是完整的代碼https://jsfiddle.net/wjugs42b/關閉腳本標籤解決了一半的問題,並在if和else之間移動一行,如果解決了另一半,你可能想給我一個投票。 –