0

我正在使用以下 HTML 代码来显示表格:

<table border="1" id="markstbl">
   <thead>
      <tr>
         <th class="answermarksth">Marks per Answer</th>
         <th class="totalmarksth">Total Marks</th>
         <th class="noofmarksth">Marks Remaining</th>
      </tr>
   </thead>
   <tbody>
      <tr class="questiontd">
         <td class="answermarkstd">
            <input class="individualMarks q1_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
         </td>
         <td class="totalmarkstd" rowspan="2">4</td>
         <td class="noofmarkstd q1_ans_text" q_group="1" rowspan="2"><strong>4</strong></td>
      </tr>
      <tr class="questiontd">
         <td class="answermarkstd">
            <input class="individualMarks q2_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
         </td>
         <td class="totalmarkstd" rowspan="1">6</td>
         <td class="noofmarkstd q2_ans_text" q_group="1" rowspan="1"><strong>0</strong></td>
      </tr>
   </tbody>
</table>

下面是表格的样子:

Marks Per Answer                Total Marks    Marks Remaining
(blank text input)                   4              4
(value=6 text input)                 6              0

我试图验证表格,以便如果文本input为空白,则显示一条消息。如果 'Marks Remaining' 不等于 0,则显示一条验证消息(如果大于 0,则显示一条验证消息,如果小于 0,则显示一条不同的验证消息)。

问题是它没有输出任何验证消息。为什么它不执行任何验证?

这是我的 jQuery 代码:

function validation() {
    var alertValidation = "";
    var _qid = "";
    var _msg = "";
    var questions = $('#markstbl td[class*="_ans"]').length;

    $("[class*='q'").each(function (i) {
        var marks = parseInt($("[class*=q" + i + "_mark]").text());
        _qid = $(".q" + i + "_ans_org").text();
        _msg = "You have errors on Question Number: " + _qid + "\n";


        if (!this.value) {
            alertValidation += "\n\u2022 You have not entered in a value in the Indivdiaul Marks textbox all your Answers\n";
        }

        if ($("[class*=q" + i + "_mark]").text() < '0') {
            alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) + " Marks";
        } else if ($("[class*=q" + i + "_mark]").text() > '0') {
            alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks + " Marks Remaining";
        }

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

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

    return true;
}​
4

1 回答 1

1

您的 jQuery 代码确实有错误。此选择器$("[class*='q'")无效。它需要是这样的: $("[class*='q']").

这是一个运行代码的 jsfiddle:http: //jsfiddle.net/jYZSX/

我仍在阅读问题以弄清楚验证应该如何工作。

于 2012-12-04T03:24:49.053 回答