我编写了一个Java脚本,用于在表单提交时提醒消息。我在很多地方都使用相同的表格。贝娄是我的js代码,
<script type="text/javascript">
var form = document.getElementById("form_demo-form");
jQuery("#form_demo-form").submit(function(e){
email = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|
(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]
{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
mb = /^((\+\d{1,3}(-|.| )?\(?\d\)?(-| |.)?\d{1,5})|(\(?
\d{2,6}\)?))(-|.| )?(\d{3,4})(-|.| )?(\d{4})(( x|
ext)\d{1,5}){0,1}$/;
if( ((form.field_qh4icy4.value !="") &&
(form.field_269dk2.value != "") &&
(form.field_1bgl12.value != "") &&
(email.test(form.field_29yf4d4.value)) &&
(mb.test(form.field_6dask2.value)) &&
(form.field_t6rya2.value != "") &&
(form.field_9jv0r14.value != "")))
{
alert("Thank you for contacting us. We have received your
enquiry. One of our representatives will respond to you
within 24 hours");
window.location.assign("/blog/thankyou");
}
else{
e.preventDefault();
}
});
</script>
但是警报消息显示了两次,而不是一次。我需要帮助。感谢进阶。
答案 0 :(得分:0)
使用e.preventDefault();在提交点击时,我已更正代码并将其粘贴在下面。试试这个。
<script type="text/javascript">
var form = document.getElementById("form_demo-form");
jQuery("#form_demo-form").submit(function(e){
e.preventDefault();
email = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|
(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]
{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
mb = /^((\+\d{1,3}(-|.| )?\(?\d\)?(-| |.)?\d{1,5})|(\(?
\d{2,6}\)?))(-|.| )?(\d{3,4})(-|.| )?(\d{4})(( x|
ext)\d{1,5}){0,1}$/;
if( ((form.field_qh4icy4.value !="") &&
(form.field_269dk2.value != "") &&
(form.field_1bgl12.value != "") &&
(email.test(form.field_29yf4d4.value)) &&
(mb.test(form.field_6dask2.value)) &&
(form.field_t6rya2.value != "") &&
(form.field_9jv0r14.value != "")))
{
alert("Thank you for contacting us. We have received your
enquiry. One of our representatives will respond to you
within 24 hours");
window.location.assign("/blog/thankyou");
}
else{
e.preventDefault();
}
});
</script>