したがって、フォームには複数の選択肢があるため、検証する必要がある回答の配列があります。コードでの私の試みはこれですが、「i」(.each() 行から) が定義されていないことを返すだけです:
function validTextAnswers(i, answer){
var answer = $j(this)
// ^ may not be needed, but I was using it
// when I was not passing arguements
if( (answer.val()=="") || (textAnswers.val()==null )){
alert('Note: Please fill in all answers.'),
answer.focus()
return false;
}
return true;
}
function validQuizCreate(){
var course = $j('#course-selection .selected')
var question = $j('textarea[name="question"]')
var textAnswers = $j('div.answer:visible').children('input[type="text"]');
if( (question.val()=="") || (question.val()==null) ){
alert('You have to supply a question.'),
question.focus()
return false;
}
if( !textAnswers.each(validTextAnswers(i, answer)) ){
console.log('all seems good!')
return false
}
私はコードをかなりいじりましたが、そうするとjQueryが壊れるだけです。(注: $j を $ と仮定します。これは、サイトの別のセクションにあるプロトタイプとの衝突を避けるためです。)
解決策:
// jQuery filter() can be used for this:
var validTextAnswers = textAnswers.filter(function(){
return this.value != '';
}).length == '4'; // four because the test is A though D.
// If all 4 multiple choice answers are not empty, it returns true.
// so to use it I can say:
if(!validTextAnswers){
alert('Please fill in all multiple choice options.')
return false;
}