event.preventDefault();提交表格时不工作

时间:2014-02-07 15:09:43

标签: javascript jquery forms

您好我试图将来自提交表单的结果放入div而不是在新窗口中打开它。问题是我的event.preventDefault();似乎没有工作,我不明白为什么。结果总是在我点击提交按钮后打开contact-form-handler.php这是脚本文件

这是代码:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

联系表格

<form class="form-container" id="contactForm" method="post" action="contact-form-handler.php">
<div class="form-title">Full Name:</div>
<input class="form-field" type="text" name="name" /><br />
<div class="form-title">Email Address:</div>
<input class="form-field" type="text" name="email" /><br />
<div class="form-title">Phone Number:</div>
<input class="form-field" type="text" name="phone" /><br />
<div class="form-title">Message:</div>
<textarea class="form-field" rows="8" cols="34" name="message"></textarea><br />
<div id="contactResponse"></div>
<div class="submit-container">
<button type="submit" class="submit-button">Send</button>
</div>
</form>

脚本代码:

<script type="text/javascript">
$("#contactForm").submit(function(event) 
     {
         /* stop form from submitting normally */
         event.preventDefault();

         /* get some values from elements on the page: */
         var $form = $( this ),
             $submit = $form.find( 'button[type="submit"]' ),
             name_value = $form.find( 'input[name="name"]' ).val(),
             email_value = $form.find( 'input[name="email"]' ).val(),
             phone_value = $form.find( 'input[name="phone"]' ).val(),
             message_value = $form.find( 'textarea[name="message"]' ).val();


         /* Send the data using post */
         var posting = $.post( "contact-form-handler.php", { 
                           name: name_value, 
                           email: email_value, 
                           phone: phone_value, 
                           message: message_value 
                       });

         posting.done(function( data )
         {
             /* Put the results in a div */
             $( "#contactResponse" ).html(data);

             /* Change the button text. */
             $submit.text('Sent, Thank you');

             /* Disable the button. */
             $submit.attr("disabled", true);
         });
    });
</script>

PHP代码:

<?php 
$myemail = 'mymail@mail.com';//<-----Put Your email address here.
$name = $_POST['name']; 
$phone = $_POST['phone'];
$email_address = $_POST['email']; 
$message = $_POST['message']; 


    $to = $myemail; 
    $email_subject = "Contact form submission: $name";
    $email_body = "Contact Form Emocool Website ".
    " Here are the details:\n Name: $name \n Telefono: $phone \n Email: $email_address \n Message \n $message"; 

    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email_address";

    if(mail($to,$email_subject,$email_body,$headers))
    {
    echo "Thank you for contacting us";
    }
    //redirect to the 'thank you' page
    else
    {
    echo "Sorry, there has been an error";  
    }

?>

所以问题是,当我点击提交按钮而不是显示在#contactResponse div中的结果时,它显示在脚本的新页面中contact-form-handler.php

为什么这不起作用的任何建议?

2 个答案:

答案 0 :(得分:3)

我最好的猜测是,您在实际呈现表单之前添加了#contactForm提交侦听器。尝试在$(document).ready(function () {});

中包装jQuery

我没有看到任何其他问题。

答案 1 :(得分:-2)

使用<script type="text/javascript">,您的脚本将无效,将<script type="text/javascript">替换为<script language="javascript">,它将正常运行。但是不推荐使用language =“javascript”,而选择type =“text / javascript”。请记住你把代码放在$(document).ready(function(){//要实现的代码//});

无论如何,您的脚本可以使用 language =“javascript”

替换 type =“text / javascript”