2014-01-11 97 views
0

表单提交后页面重新加载,我希望它不能重新加载。 更新:在下面的答案我想出了下面的代码。表单提交正确,但页面重新加载会发生什么。 之前,这行代码工作得很好:Ajax表单提交重新加载页面

$(document).ready(function() { 
    $('#poll').ajaxForm(function() { 
     $("#polling_id").load("poll_results.php"); 
    }); 
}); 

今天,我已经添加的.htaccess删除扩展名为.php。这可能会影响到这一点吗?

HTML

<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></SCRIPT> 
</head> 
<div class="polling" id="polling_id"> 
    <br><br>  

    <form id="poll_form" method="POST" action="process-vote.php" /> 
    <div class="poll_objects"> 
     <input type="hidden" name="o1" value="<?php echo $option1; ?>" /> 
     <input type="hidden" name="o2" value="<?php echo $option2; ?>" /> 
     <input type="hidden" name="o3" value="<?php echo $option3; ?>" /> 
     <span class="footer_font"><input type="radio" name="radio_1" value="<?php echo $option1;?>" id="radio" /><label for="radio"><?php echo $option1;?></label> </span><br><br> 
     <span class="footer_font"><input type="radio" name="radio_1" value="<?php echo $option2;?>" id="radio2" /><label for="radio2"><?php echo $option2;?></label></span><br><br> 
     <span class="footer_font"><input type="radio" name="radio_1" value="<?php echo $option3;?>" id="radio3" /><label for="radio3"><?php echo $option3;?></label></span><br><br> 
     <div class="float_buttons"> 
      <input type="submit" name="submit_vote" value="Vote!" class="button" /> 
      <input type="submit" name="results" value="Poll Results" class="button"/> 
     </div> 
    </div> 
</div> 
</form> 

的jQuery:

<script> 
    $(document).ready(function() {   
     $.ajax({  
      $('#poll_form').submit(function(e) { 
       e.preventDefault(); 
       // note where the e comes from. 
       var a = $(this).serialize(); 
       $("#polling_id").load("poll-results.php"); 
      }); 
     });  
    }); 
</script> 
+1

你已经取代线顺序:)'$阿贾克斯({'和'e.preventDefault(;' –

回答

3

它看起来并不像你拍摄的事件。

$('#poll_form').submit(function(e) { 
    e.preventDefault(); 
    // note where the e comes from. 
    var a = $(this).serialize(); 
    // now do your ajax stuff here. 
}); 
+0

你的方法加载的东西,而页面刷新,但它不提交表格 – user3186034

+0

这是因为你必须实施必要的'$ .ajax({})'调用来发送数据到你的服务器并作出适当的响应 –

+0

以及提交的表单和页面重新加载。我已更新我的帖子。 – user3186034

0

你也可以把:

<script> 
    function validate() 
    { 
    ... 
     blablabla 
    ... 
    return false; // This cause the submit function to be stopped. 
    } 
</script> 
<form onClick="validate()">