我正在设计一个JavaScript函数 checkForm(),用于检查表单的至少一半答案是否已完成。如果条件满足则返回“true”并发出警告消息,否则返回“false”。答案在输入文本字段中给出,其内容不能是空字符串,以便被视为已回答。
为了进行检查,函数 checkForm()调用另外两个函数: totalNumberOfQuestions()和 totalCompletedAnswers(),如如下:
function totalNumberOfQuestions() /* Returns the total number of questions in the form */
{ total
numberOfQuestions = 0;
i = 1;
do
{
nameOfAnswer = 'answer_' + i;
if(document.getElementsByName(nameOfAnswer)[0] != null)
{
totalnumberOfQuestions ++;
}
i++;
}
while (document.getElementsByName(nameOfAnswer)[0] != null);
return totalnumberOfQuestions;
}
function totalCompletedAnswers() /* Returns the total number of completed answers in the form */
{
numberOfCompletedAnswers = 0;
i = 1;
for(i = 1; i<= totalNumberOfQuestions(); i++)
{
nameOfAnswer = 'answer_' + i;
if ( form[nameOfAnswer].value != '')
{
numberOfCompletedAnswers ++;
}
}
return numberOfCompletedAnswers;
}
function checkForm()
{
if ( totalCompletedAnswers() < 7 )
{
alert("Please answer at least half of the questions.");
return false;
}
return true;
}
但令人惊讶的是,函数 checkForm()在此格式中无法正常工作,但即使已完成答案的数量少于答案总数的一半,也会返回true。但是,如果针对常量值检查函数 totalCompletedAnswers(),则该函数可正常工作,例如:
function checkForm()
{
if ( totalCompletedAnswers() < 7 )
{
alert("Please answer at least half of the questions.");
return false;
}
return true;
}
这意味着在 checkForm()函数的主体中使用函数 totalNumberOfQuestions()是“导入”一些不需要的返回值。即使我单独调用前者( x = totalNumberOfQuestions()然后重写条件: if(totalCompletedAnswers()&lt; x),也会发生这种情况 )。有谁知道在调用函数时如何避免这些不需要的返回值?
答案 0 :(得分:0)
在totalComletedAnswers
中你有片段:
if(numberOfCompletedAnswers < totalNumberOfQuestions() )
{
alert("Please answer at least half of the questions.");
return false;
}
如果所有答案都没有完成,那么totalCompletedAnswers
将返回false 不回答的问题数量。
在checkForm
中,以下内容将评估为true:false < 7
请参阅:http://jsfiddle.net/5mURZ/
有关javascript中的真实性/虚假性的更多信息,请查看这两篇文章:
<强>更新强>
仔细检查后,您会遇到范围问题。您的所有变量都具有全局范围。这将导致变量i
出现大多数问题。使用var
关键字声明变量会将变量的范围限制为函数。在javascript中最好使用var
关键字来提取所有变量,以明确声明变量的范围。
工作小提琴:http://jsfiddle.net/Dr5Hx/1/
关于范围界定的文章:http://coding.smashingmagazine.com/2009/08/01/what-you-need-to-know-about-javascript-scope/
更新2
对于完全不同的方法,请尝试jQuery的魔力。这种方法依赖于将答案包装在某种容器中。在这种情况下,div
。这是必需的,因为复选框和单选按钮等元素将是多个元素,但每个集合只应计算一次。
//The selector below finds DOM elements with a name beginning with "answer"
var answers = $("[name^='answer']"); //'Cache' answers
var answerDivs = $("div").has("[name^='answer']"); //'Cache' answer divs
var totalAnswers = answerDivs.length; //'Cache' number of answers
$("#checkIt").click(function(){ //Add a click listener to the button
var numAnswered = $(answerDivs).filter(function(){ //Filter our answer divs to answered question
var answer = $(this).find("[name^='answer']"); //Get the answer element(s) in the div
var textCheck = $(answer).is("input:text") && $(answer).val() != ''; //Check For Text Value
var selCheck = $(answer).is("select") && $(answer).val() != ''; //Check for selected
var radioCheck = $(answer).is(":checked"); // Check for Checked, WOrks for radio & checkbox
//console.log($(answer).attr("name") + " " + textCheck + " " + selCheck + " " + radioCheck)
return (textCheck || selCheck || radioCheck); //End of the filter
}).length;
if(numAnswered < (totalAnswers/2))
{
alert("Please answer atleast half the questions");
return false;
}
alert("We're good to go");
return true;
});
请确保将上述内容包含在$(document).ready();