我正在编写一个将验证注册表单字段的 javascript 片段。由于某种原因,onsubmit 函数在页面加载时也会被调用。
window.onload = function validate_signup_form() {
var form_element = document.getElementById("signup_form");
var onload_values = document.forms["signup_form"];
var test = onload_values.elements[0].value;
var required_fields = new Array();
document.getElementById("signup_form").onsubmit = check_values();
function check_values()
{
var current_values = document.forms["signup_form"];
for (x in current_values)
{
if (current_values[x].value == onload_values[x].value)
{
// Error: The form values match the initial ones
return false;
}
elseif (current_values[x].value.toLowerCase() == onload_values[x].value.toLowerCase())
{
// Error: The form values match the initial ones
return false;
}
elseif (current_values[x].value.toUpperCase() == onload_values[x].value.toUpperCase())
{
// Error: The form values match the initial ones
return false;
}
}
};
};
不应该 check_values(); 仅在提交时运行?