2013-02-21 36 views
0

我遇到了這個函數的問題,第一部分被稱爲罰款,但在第一個if語句之後沒有其他任何東西被調用。我已經使用JSfiddle,但它不能確定一個嚴重的問題。Javascript函數的第一部分被稱爲

我通常使用PHP不是JS,所以我想知道是否有簡單的東西,我在這裏失蹤?

function validatequestion(form){ 
     var e = document.getElementById("chooseqtype"); 
     var strQtype = e.options[e.selectedIndex].value; 

     if(strQtype == "default"){ 
      alert("Please select a question type"); 
      return false; 
     } 

     if(strQtype == "textquestion"){ 
      fail = validatetextq(form.textquestiondesc.value) 
      if(fail == "") return true 
      else { 
       alert(fail); 
       return false; 
      } 
     } 

     if(strQtype == "videoquestion"){ 
      fail = validatevideoq(form.videoquestiondesc.value) 
      if(fail == "") return true; 
      else { 
       alert(fail); 
       return false; 
      } 
     } 

     //everything above works, after this point nothing seems to get called 
     var a = document.getElementById("chooseatype"); 
     var strAtype = a.options[a.selectedIndex].value; 

     if(strAtype == "textanswer"){ 
      //get the value of the number of text answers select box 
      var t = document.getElementById("choosetextnumber"); 
      //put the value in variable strQtype 
      var strTextno = t.options[t.selectedIndex].value; 


      if(strTextno == "2tanswers"){ 
       fail = validatetexta1(form.textanswer1.value) 
       fail += validatetexta2(form.textanswer2.value) 
       if(fail == "") return true; 
       else { 
        alert(fail); 
        return false; 
       } 
      } 

     } 




     } 
+0

你從第一個返回false if – 2013-02-21 13:45:14

+0

NITPICK:使用else if! – epascarello 2013-02-21 13:45:27

+0

你究竟在用這段代碼實現什麼? – Manuel 2013-02-21 13:47:01

回答

0

如果strQtype只能是你正在測試的3個值之一,那麼有沒有辦法可以永遠得到你的代碼,因爲你總是從每個那些if語句返回的第二部分。

編輯:

你需要做的是returnfail == ""。由於你只是返回true我假設你不需要返回一個值,只需驗證驗證是否成功。你應該做的只是測試失敗,例如if (! fail=="")(語法錯誤,JavaScript不是我的第一個語言),在這種情況下請執行alert

或者你總是可以只寫3個不同的功能,一個測試每個菜單項,這是我可能會做的。

+0

感謝您的回答,但這種方法的問題是必須始終選擇三個菜單。有一個菜單可以選擇想要製作什麼類型的問題,一個菜單可以選擇要製作的答案類型,以及一個菜單來選擇你做出多少答案。 – user1953507 2013-02-21 14:06:15

+1

那麼如果是這樣的話,那麼它回到了我原來的觀點,即你從那些你永遠不會超過他們的前3個if語句中返回**。您需要保存他們爲第一個菜單選擇的答案,即他們想要製作什麼類型的問題,然後繼續前進,以便查看他們想要的答案類型。 – knoight 2013-02-21 14:08:55

+0

看到我的編輯澄清。 – knoight 2013-02-21 14:22:36

相關問題