gpt4 book ai didi

javascript - Ajax 表单提交重新加载页面

转载 作者:行者123 更新时间:2023-11-28 20:04:31 25 4
gpt4 key购买 nike

页面在表单提交后重新加载,我希望它不要重新加载。更新:根据下面的答案,我想出了下面的代码。发生的情况是表单正确提交,但页面重新加载。之前,这行代码运行良好:

$(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>

最佳答案

您似乎没有捕获事件。

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

关于javascript - Ajax 表单提交重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21067992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com