1
在以下代碼中,如何基於成功的addLoadEvent(check_email)同步調用do_something() ?圍繞它在 if(){}不起作用。我需要使用Event/EventListener嗎?如果是這樣,怎麼樣?如何根據表單域檢查來協調Javascript方法調用?
的index.html:
<!DOCTYPE html >
<html>
<head>
<script type="text/javascript" src="foo.js"></script>
</head>
...
<form action="#">
<label for="email">Enter email address <img src="email.gif" alt="" /></label>
<input class="email_class" id="email" value="" />
...
<input type="submit" value="Submit">
</form>
</html>
foo.js:
function check_email()
{
in_Forms = document.getElementsByTagName("form");
for (var i = 0; i < in_Forms.length; i++)
{
in_Forms[i].onsubmit = function()
{
return check_this_email(this);
}
}
}
function check_this_email(some_email) {
var ret_val = false;
var in_Inputs;
in_Inputs = some_email.getElementsByTagName("input");
for (var i = 0; i < in_Inputs.length; i++) {
if (in_Inputs[i].className == "email_class")
{
if (bad_email_check ...)
{
ret_val = true;
alert('enter valid email');
}
}
}
return ret_val;
}
function addLoadEvent(some_function)
{
var oldonload = window.onload;
if (typeof window.onload != 'function')
{
window.onload = some_function;
}
else
{
window.onload = function()
{
oldonload();
some_function();
}
}
}
addLoadEvent(check_email);
if (email_address_correctly_entered){
do_something();
}
問題不清楚。添加更多細節。 – Manwal
我正在尋找一種機制,只有在電子郵件字段驗證成功後纔會調用do_something()方法*,然後通過按「SUBMIT」按鈕觸發該機制。所以,這應該是任務的順序:表單加載,電子郵件字段被填充並且「SUBMIT」按鈕被推送,如果電子郵件字段值正確,請調用do_something()。 – user4856216