0

当问题中的任何文本输入为空时,我想在警报中生成验证消息。因此,例如,如果问题 1 有 2 个空白文本输入,如果两个文本输入均为空白,则会显示验证消息You have not entered in a value in all the Indivdiaul Marks textbox

但问题是,例如,对于问题 1,如果 1 个文本输入为空白但另一个不是空白,则它不会显示验证消息,即使它应该这样做,因为并非所有文本输入都已为问题 1 填写。

我的问题是,如果每个问题有任何空白文本输入,我如何才能显示验证消息?

这是一个小提琴,因此您可以对其进行测试:http: //jsfiddle.net/cbyJD/87/

下面是validation()函数代码:

function validation() {

    // only keeping track of the final message
    var alertValidation = "",
        // toggle for showing only one error
        showOnlyOneError = true;

    $("input[data-type='qmark']").each(function(i) {  
        var questions = $(this).attr("data-qnum");
        var marks = parseInt($("[class*=q" + (i+1) + "_ans_text]").text()); 
        var txtinput = $(this).val(); 

        // the message for this question
        var msg = '';

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

        if (marks < 0) {
            msg += "\n\u2022 Your Total Marks Remaining does not equal 0 \n - You Need To Remove " + Math.abs(marks) +  " Marks";   
        } else if (marks > 0) {
            msg += "\n\u2022 Your Total Marks Remaining does not equal 0 \n - You Have " + marks +  " Marks Remaining";   
        }

        // if there is an error for the question, add it to the main message
        if (msg.length) {
            alertValidation += alertValidation.length ? '\n\n' : '';
            alertValidation += "You have errors on Question Number: " + questions + "\n";
            alertValidation += msg;
            // stop if we only care about the first error
            return !showOnlyOneError;
        }
    });

    // show the error messages
    if (alertValidation != "") {
        alert(alertValidation);
        return false;
    }

    return true;
}
4

2 回答 2

1

num_groups首先,添加一个包含组总数的隐藏变量。

<p>
  <input type='hidden' id='num_groups' name='num_groups' value='2'>
  <input id="submitBtn" name="submitMarks" type="submit" value="Submit Marks" />
</p>

其次,将validate函数更改为处理一组问题:

function validation(group) {
  var msg = [];

  var nb = 0;     // Number of blank values
  $("input[data-qnum='" + group + "']").each(function() {
    if ($(this).val() == '') {
      nb++;
      return false;
    }
  });
  if (nb != 0) {
    msg.push("\u2022 You have not entered in a value in all the Individual Marks textbox");
  }

  var marks = parseInt($("[class*=q" + group + "_ans_text]").text()); 
  if (marks != 0) {
    msg.push("\u2022 Your Total Marks Remaining does not equal 0");
    if (marks < 0) {
      msg.push("- You Need To Remove " + -marks + " Marks");
    } else if (marks > 0) {
      msg.push("- You Have " + marks + " Marks Remaining");
    }
  }

  if (msg.length > 0) {
    alert("You have errors on Question Number: " + group + "\n\n" + msg.join("\n"));
    return false;
  } else {
    return true;
  }
}

第三,更改myClickhandler函数以验证所有组:

myClickHandler = function(e) {
  var ng = $('#num_groups').val();
  for (var group = 1; group <= ng; group++) {
    if (!validation(group)) return false;
  }
  if (confirm("Are you sure you want to Proceed?")) {
    $.ajax({
      url: "insertmarks.php",
      data: $("#markstbl").serialize(),
      async: false,
      type: "POST"
    });
    return true;
  } else {
    return false;
  }
}

JsFiddle在这里

于 2012-12-25T22:06:25.157 回答
0

无论找到什么,该validation()函数都会返回。true

您需要在此函数的顶部声明一个名为errors或类似的变量,其值为false. 对于在函数中发现错误的任何地方,将 的值设置errorstrue

在函数结束时...

return !errors; // return the value of errors NOT'ed

然后你在测试中调用 call validation(),就像这样......

if (validation()) {
    // everything is groovy
} else {
    // make them do the form over, or something
}
于 2012-12-23T05:57:10.870 回答