2012-02-05 91 views
0

我有一個應用程序here如何寫這個if語句?

當你打開應用程序,請繼續點擊「添加問題」按鈕,你會發現,它不會讓你添加超過5個錶行。這是因爲這個代碼在這裏:

if (qnum > 5) { 
    return; 
} 

這行代碼進入低於該控件添加錶行的功能:

var qnum = 1; 

function insertQuestion(form) { 


    if (qnum > 5) { 
    return; 
} 


    var $tbody = $('#qandatbl > tbody'); 
    var $tr = $("<tr class='optionAndAnswer' align='center'></tr>"); 
    var $qid = $("<td class='qid'>" + qnum + "</td>"); 


    $tr.append($qid); 
    $tbody.append($tr); 


    $(form).find('.numberOfQuestions').val(qnum); 

    ++qnum; 
    $("#questionNum").text(qnum); 
    form.questionText.value = ""; 


} 

添加的行進入此表在這裏:

<table id="qandatbl" align="center"> 
<thead> 
<tr> 
    <th class="qid">Question No</th> 
</tr> 
</thead> 
<tbody></tbody> 
</table> 

我的問題是,我將創建一個新的函數,稱爲驗證()函數,這將創建警報。我的問題是,如何編寫if語句來聲明如果最後一個問題號碼不在表格行中,則會在警報中顯示「您尚未添加所有問題\ n您有問題」。我如何編寫if語句?

下面是當前驗證的時刻()函數:

function validation() { 

    var marks = parseInt($("#total-weight").text());  
    var _qid = ""; 
    var _msg = ""; 

    var maxQuestions = <?php echo (int)@$_POST['textQuestion']; ?>; 
    var questionsAdded = $('tr.optionAndAnswer').length; 


    var alertValidation = ""; 
    // Note, this is just so it's declared... 
    $("tr.optionAndAnswer").each(function() { 


     _qid = $("td.qid",this).text(); 
     _msg = "You have errors on Question Number: " + _qid + "\n"; 

     $(".textAreaQuestion",this).each(function() { 



      if (!this.value || this.value.length < 5) { 
       alertValidation += "\n\u2022 You have not entered a valid Question\n"; 
      } 

      if (alertValidation != "") { 
       return false; //Stop the each loop 
      } 
     }); 

     $(".numberAnswerTxtRow",this).each(function() { 


      var currenttotal = $(this).closest('.optionAndAnswer').find('.answerBtnsOn').length; 

      if (!this.value) { 
       alertValidation += "\n\u2022 Please Enter in the Number of Answers you Require for this question\n"; 
      } 

      else if (currenttotal > $(this).val()){ 
       alertValidation += "\n\u2022You have selected more answers than the required amount\n"; 
    } 

      else if (currenttotal < $(this).val()) { 
       alertValidation += "\n\u2022 You have selected less answers than the required amount\n"; 
    } 

      if (alertValidation != "") { 
       return false; //Stop the each loop 
      } 

     }); 

     $(".txtWeightRow",this).each(function() { 


      if (!this.value) { 
       alertValidation += "\n\u2022 Please enter in a figure for Number of Marks for this Question\n"; 
      } 

      if (alertValidation != "") { 
       return false; //Stop the each loop 
      } 
     }); 


     if(alertValidation != ""){ 
      return false; 
     } 
    }); 

if(alertValidation == ""){  
      if($("#total-weight").text() < '0') 
{ 
    _msg = ''; 
alertValidation = "Your Total Session Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) + " Marks"; 
} 

     else if($("#total-weight").text() > '0') 
{ 
    _msg = ''; 
alertValidation = "Your Total Session Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks + " Marks Remaining"; 
} 

     else if(questionsAdded < maxQuestions){ 
    msg = ''; 
    alertValidation("You Have Not Added in All of Your Questions. You have " + (maxQuestions - questionsAdded) + " Questions Remaining:"); 
} 


} 

    if (alertValidation != "") { 
     alert(_msg + alertValidation); 
     return false; 
    } 

    return true; 
} 

回答

1

您可以檢查沒有。添加的行數和comapre不論你擁有的最大數量。嘗試這個。

var maxQuestions = 5; 
var questionsAdded = $('tr.optionAndAnswer').length; 
if(questionsAdded < maxQuestions){ 
    alert("Questions remaining: " + (maxQuestions - questionsAdded)); 
} 
+0

嗨,它不顯示此警報的消息。我希望此訊息出現在#總重量<或<0之前。當該錯誤已解決時,顯示#總重量<或<0的訊息,但目前未顯示訊息問題剩餘...我在我的問題中包含了我當前的驗證()函數編輯 – user1181690 2012-02-05 23:39:32

+0

您的代碼更改存在錯誤。而不是'='你已經把'('。看看這一行'else if(questionsAdded ShankarSangoli 2012-02-05 23:54:35

+0

哦,等一下,我意識到我錯過了一個= sign。 – user1181690 2012-02-05 23:55:19