简体   繁体   English

jquery验证不适用于相关的html

[英]jquery validation is not working with relevant html

http://jsfiddle.net/7sARw/17/ http://jsfiddle.net/7sARw/17/

Above is a jsfiddle of my application. 上面是我的应用程序的jsfiddle。 I apologise that there is a lot of code in the fiddle but the code is needed to get the app working and I have really tried to cut the code down a lot. 我很抱歉这个小提琴中有很多代码,但需要代码才能让应用程序正常工作,我真的试图将代码削减很多。

But please use the jsfiddle by following the steps below: 但请按照以下步骤使用jsfiddle:

  1. When you open fiddle, click on the "Open Grid" link and select the "True or False" option. 当您打开小提琴时,单击“打开网格”链接并选择“正确或错误”选项。 You will see "True" and "False" buttons appear and a text input which states 1. 您将看到“True”和“False”按钮出现,文本输入显示1。

  2. If you click on the "Add Question" button, an alert appears stating you have selected less answers than the require amount. 如果单击“添加问题”按钮,则会显示一条警告,表明您选择的答案少于要求金额。 This is true as the text input states you need 1 number of answers but you havn't selected an answer. 这是正确的,因为文本输入表明您需要1个答案,但您没有选择答案。 So Select either "True" or "False" then click on the "Add Question" button. 因此,选择“True”或“False”,然后单击“添加问题”按钮。

  3. You will see that it appends a row into the table of what you have entered. 您将看到它在您输入的内容的表格中附加了一行。 Now within the row turn off the selected button so that neither "True" or "False" button is turned on. 现在在行内关闭所选按钮,以便既不打开“True”或“False”按钮。 Now at bottom of application click on the "Submit Details" button. 现在在应用程序的底部,单击“提交详细信息”按钮。 You realise that no alert appears stating you selected less answers. 您意识到没有出现警告,表明您选择的答案较少。

This is the problem I am having, how came when I click on the "Submit Details" button that no alert appears stating that you have selected less answers? 这就是我遇到的问题,当我点击“提交详细信息”按钮时,如果没有出现任何警告声明您选择的答案较少? I know that the submit button will display other validation messages so there is nothing wrong on how I am calling the function, it is within the validation() function where I am trying to validation number of answers selected where I am having a problem. 我知道提交按钮会显示其他验证消息,所以我如何调用该函数没有任何错误,它在validation()函数内,我正在尝试验证我遇到问题时选择的答案数。

Below is the code for the validation which is at the bottom of the jsfiddle: 下面是jsfiddle底部验证的代码:

 function validation() {

    var marks = parseInt($("#total-weight").text());
    var _qid = "";
    var _msg = "";

    var maxQuestions = 5;
    var questionsAdded = $('tr.optionAndAnswer').length;

    var alertValidation = "";
    // Note, this is just so it's declared...
    $("tr.optionAndAnswer").each(function () {


        _qid = $("td.qid", this).text();
        _msg = "You have errors on Question Number: " + _qid + "\n";

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


            var currenttotal = $(this).closest('.optionAndAnswer').find('.answerBtnsOn').length;

            if (currenttotal < $(this).val()) {
                alertValidation += "\n\u2022 You have selected less answers than the required amount\n";
            }

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

        });



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



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

    return true;
} 

This appears to work: http://jsfiddle.net/7sARw/20/ 这似乎工作: http//jsfiddle.net/7sARw/20/

It seems that you weren't attaching a click handler to call validation() . 您似乎没有附加单击处理程序来调用validation() I added this code in your $(document).ready function: 我在$(document).ready函数中添加了这段代码:

$('#QandA').submit(function (evt) {
   return validation();
});

This says, submit the form if validation() returns true, and otherwise don't submit it. 这就是说,如果validation()返回true,则提交表单,否则不提交。 Now if you add a question, change the Number of Answers field to 2, and submit, the alert shows up. 现在,如果您添加问题,将“答案数”字段更改为2并提交,则会显示警报。

Type mismatch error: 类型不匹配错误:

<table id="optionAndAnswer" class="optionAndAnswer">

$("tr.optionAndAnswer").each(function() {

<table> id is optionAndAnswer , class is also optionAndAnswer , oke fine not an error, but you jQuery selector is selecting an <tr> with the class optionAndAnswer , and this one is not present in you HTML <table> idoptionAndAnswerclass也是optionAndAnswer ,oke罚款不是错误,但你的jQuery选择器正在选择带有optionAndAnswer class<tr> ,而你的HTML中没有这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM