2012-02-05 84 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