2012-02-04 75 views
0

在此jsfiddle的方式,请按照下列步骤操作:警报不工作,我希望它的工作

1:当您在“添加问题”按钮,打开小提琴点击两次,这将下面添加2行。

2:点击“提交信息”按钮,它会想出这个错误:

You have errors on Question Number: 2

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 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

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: 2

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

上述警告是不正确的它指出错误的错误问题编号。问题编号应该是数字1,而不是数字2.那么,如何解决这个问题。

下面是其中的警报控制的验证()函数的代码:

function validation() { 

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

    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"; 

     $(".textAreaQuestion",this).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",this).each(function() { 


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

      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",this).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 
      } 
     }); 
    }); 

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

    return true; 
} 

回答

1

你必须检查是否在外环($("tr.optionAndAnswer").each())已经发现任何错误,通过返回false停止。

在每个外部循环中添加此项,并声明alertValidationvar alertValidation否则它将位于全局范围内。

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

我已经修复它在你的小提琴演示看看。

http://jsfiddle.net/ShankarSangoli/euL5e/1/

+0

谢谢你,这已经非常完美 – user1182476 2012-02-04 20:41:08

1

我想你刚刚退出的$("tr.optionAndAnswer").each万一alertValidation有内容(可以更换,如果(alertValidation != "")if (alertValidation))。

看一看http://jsfiddle.net/GgvEs/

+0

Upvot的答案,这个答案也是正确的,谢谢 – user1182476 2012-02-04 20:40:41