2012-02-05 11 views
0

私はあなたがアプリケーションを開くと、「質問を追加」ボタンをクリックして保管してくださいアプリケーションhereif文を書くには?

を持って、あなたはそれがあなたが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> 

私の質問は、validation()関数と呼ばれる新しい関数を作成し、アラートを作成することです。どのような私の質問はどのように行う、私は文が最後の質問の数は、テーブルの行にない場合、アラートで表示することを述べることならば、「「あなたは、あなたの質問のすべてを追加していない\ 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

以下は、現在検証現時点では()関数です。あなたが持っている最大の番号と比較して、より多くの行が追加されました。これを試して。

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

こんにちは、このアラートのメッセージは表示されません。このメッセージが#total-weight>または<0よりも前に表示されるようにします。そのエラーが解決されたら、#total-weight>または user1181690

+0

コード変更にエラーがあります。代わりに、 'の='あなたは '(' 'この行を見て入れているそれ以外の場合(questionsAdded ShankarSangoli

+0

ああ、minueを待って、私は=記号を逃した実現し、あなたの助けのための – user1181690