我创建了一个表单,似乎验证得很好。
由于onclick按钮,我无法提交页面。这已经让我有点困惑,并希望将命令放在javascript中。
如何在javascript中编写它?
<script language="javascript">
function button_actions(){
if (!document.my_form.referrers_name.value){
alert('You need to supply your Referrers name.');
document.my_form.referrers_name.focus();
return false;
}
if (!document.my_form.referrers_organisation.value){
alert('You need to supply your Referrers organisation.');
document.my_form.referrers_organisation.focus();
return false;
}
if (!document.my_form.referrers_email_address.value){
alert('You need to supply your Referrers email address.');
document.my_form.referrers_email_address.focus();
return false;
}
if (!document.my_form.referrers_phone_number.value){
alert('You need to supply your Referrers phone number.');
document.my_form.referrers_phone_number.focus();
return false;
}
if (!document.my_form.referral_document.value){
alert('You need to supply your Referral Document.');
document.my_form.referral_document.focus();
return false;
}
if (!document.my_form.name_of_contact.value){
alert('You need to supply a Name of contact.');
document.my_form.name_of_contact.focus();
return false;
}
if (!document.my_form.email_of_contact.value){
alert('You need to supply an Email of contact.');
document.my_form.email_of_contact.focus();
return false;
}
if (!document.my_form.phone_of_contact.value){
alert('You need to supply a Phone number of contact.');
document.my_form.phone_of_contact.focus();
return false;
}
if (!document.my_form.details.value){
alert('You need to supply further details.');
document.my_form.details.focus();
return false;
}
}
</script>
答案 0 :(得分:1)
不要使用onclick
在按钮上启动该功能,而是尝试使用onsubmit
在表单上触发该功能。
<form name="my_form" method="post" action="assign any page to this" onsubmit="return button_actions();">
然后只需使用简单的提交按钮即可触发onsubmit
:
<input type="submit">
表单现在会在通过验证时提交。