0

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

<table border='1' id='markstbl'>
    <thead>
    <tr>
    <th class='questionth'>Question No.</th>
    <th class='answermarksth'>Marks per Answer</th>
    </tr>
    </thead>
    <tbody>

    <tr class="questiontd">
    <td class="questionnumtd" name="numQuestion" rowspan="2">1 <input type="hidden" name="q1_ans_org" class="q1_ans_org" value="5"><input type="hidden" name="q1_ans" class="q1_ans"></td>
    <td class="answermarkstd">
    <input class="individualMarks q1_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />
    </td>
    </tr>

    <tr class="questiontd">
    <td class="answermarkstd">
    <input class="individualMarks q1_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />
    </td>
    </tr>

    <tr class="questiontd">
    <td class="questionnumtd" name="numQuestion" rowspan="1">2 <input type="hidden" name="q2_ans_org" class="q2_ans_org" value="5"><input type="hidden" name="q2_ans" class="q2_ans"></td>
    <td class="answermarkstd">
    <input class="individualMarks q2_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />
    </td>
    </tr>
    </tbody>
    </table>

下面是表格的样子:

Question No.     Marks Per Answer         
    1            (blank text input)       
                 (blank text input)
    2            (blank text input)       

如您所见,问题 1 包含 2 个文本输入,而问题 2 包含 1 个文本输入。

现在我想要对下面的代码做的是验证任何空文本输入。

function validation() {

    var alertValidation = "";
    var _qid = "";
    var _msg = "";

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


if (txtinput == '' || txtinput == null) {
    alertValidation += "\n\u2022 You have not entered in a value for all the Indivdiaul Marks textbox\n";
}    

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

        });


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

    return true;
}

问题是在警报中它不显示问题编号_qid,因为它显示为 NaN 并且即使没有文本输入是空白的,它仍然会显示一个警报,指出并非所有输入都包含一个值。

我的问题是如何显示问题编号以及如何正确检查空文本输入是否正确,以便如果文本输入为空,则显示带有正确问题编号的警报,并且如果所有文本输入都包含一个值,那么它不显示验证?

我想说的是_qid,当我在函数中包含下面的代码以进行另一个验证检查时,它确实有效,该validation()函数恰好位于 if 语句检查空文本输入的下方:

if(marks < '0')
{

 alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) +  " Marks";   
}

        else if(marks > '0')
{

 alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks +  " Marks Remaining";   
}

当出现上述验证的警报时,它会在警报中显示问题编号。当我验证空文本输入时它只是不显示它?是因为我可能在一个问题中有多个文本输入,所以我可能需要在代码中包含另一个 foreach() 循环以进行空文本输入?

下面是完整Validation()代码的样子:

function validation() {

    var alertValidation = "";
    var _qid = "";
    var _msg = "";

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


if (txtinput == '' || txtinput == null) {
    alertValidation += "\n\u2022 You have not entered in a value for all the Indivdiaul Marks textbox\n";
}    


if(marks < '0')
{

 alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) +  " Marks";   
}

        else if(marks > '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;
}

以下是验证消息的示例:

标记高于 0 验证:

You have errors on Question Number: 1
Your Total Marks Remaining does not equal 0 

• You Have NaN Marks Remaining

标记小于 0 验证:

You have errors on Question Number: 2
Your Total Marks Remaining does not equal 0 

• You Need To Remove NaN Marks

空文本输入验证:

You have errors on Question Number: NaN

• You have not entered in a value for all the Indivdiaul Marks textbox

如您所见,上面的警报是唯一不显示问题编号的警报

谢谢

4

0 回答 0