以下是我的验证功能:
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
}
});
上面代码的作用是,如果行中有任何错误,它会为每一行显示警告。
示例:
如果在表格行中,用户已将.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);
}
由于
答案 0 :(得分:1)
好了,因为您将类 qid 添加到包含问题编号的td,您应该可以通过$('td.qid').html()
访问它。这为您提供了td的innerhtml,其中包含 qid 类,请查看here以获取更多信息。
修改强>
第二个想法$('td.qid').text()
可能会更好。