2012-02-03 77 views
0

顯示錶中的行數下面是我的驗證功能:如何在警報

function validation() { 


    alertValidation= ""; 
     // Note, this is just so it's declared... 

    $(".textAreaQuestion").each(function() { 
     if (!this.value || this.value.length < 5) { 
      alertValidation += "\nYou have not entered a valid Question\n"; 
     } 

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

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

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

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

    }); 

     $(".txtWeightRow").each(function() { 
     if (!this.value) { 
      alertValidation += "\nPlease enter in a figure for Number of Marks for this Question\n"; 
     } 

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

什麼上面的代碼所做的是,它會顯示警告的每一行,如果有行中的任意errros。

實施例:

如果表中的行該用戶已離開.textAreaQuestion,.numberAnswerTxtRow和.textWeightRow所有空的,那麼在警報它會顯示所有相關的消息的在一個警報象下面這樣:

You have not entered a valid Question 

Please Enter in the Number of Answers you Require for this question 

Please enter in a figure for Number of Marks for this Question 

現在每個表格行都有自己的問題編號(表格行號)。因此,我想知道的是,如何將問題編號包含在警報中,以便指出警報消息所引用的行?如果是像上面的例子我想要顯示像下面的警告:

You have errors on question number: 1 // how do I display this line in the alert 

You have not entered a valid Question 

Please Enter in the Number of Answers you Require for this question 

Please enter in a figure for Number of Marks for this Question 

下面是對問題編號是如何被添加到每個錶行的代碼:

var qnum = 1; 

function insertQuestion(form) { 

    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); 

} 

感謝

回答

1

既然您已將類qid添加到包含問題編號的td中,您應該可以通過$('td.qid').html()訪問它。這給你的td的innerhtml類qid檢查here欲瞭解更多信息。

編輯: 關於第二個想法$('td.qid').text()可能會更好。