我遇到了表单在提交时刷新的问题,我不希望它刷新,但我确实希望它提交。你们知道我能做什么吗?
单击此链接可查看有关此内容的旧帖子。
<form method="post" id="radioForm">
<?
foreach($result as $radio):
printf('
<button type="submit"
href="#radio"
name="submitRadio"
value="'.$radio['id'].'">
Go!
</button>
');
endforeach;
?>
</form>
<script type="text/javascript">
$('#radioForm').submit(function(event) {
event.preventDefault();
$.ajax({
url:'index.php',
data:{submitRadio:[radiovalue]},
type:'POST',
success:function(response) {
/* write your code for what happens when the form submit */
});
});
</script>
</div>
使用submit()处理程序并将按钮的值传递给其他脚本
首先在表单上设置id。
<form method="post" id="formId">
然后绑定一个监听器
$( "#formId" ).submit(function( event ) {
event.preventDefault();
//This is where you put code to take the value of the radio button and pass it to your player.
});
要使用它,您需要jQuery。
您可以在此处阅读有关此处理程序的更多信息:http://api.jquery.com/submit/
这是HTML <form>
在提交时的默认行为,它使浏览器POST
数据到达action
属性中指定的目标位置,并将处理结果加载给用户。
如果您想在不重新加载页面的情况下在后台提交表单和POST
值,则必须禁用默认行为(表单提交)并使用AJAX
。这种功能在各种JavaScript库中都很容易获得,例如一个名为jQuery
的通用库。
以下是jQuery AJAX功能的文档http://api.jquery.com/jquery.ajax/
有很多关于interwebs的教程,可以向您介绍jQuery的基本用法(将库包含在HTML页面中),以及如何通过AJAX提交表单。
您需要创建一个PHP文件,该文件可以获取AJAX请求发布的值(例如将值提交到数据库)。该文件需要返回可以在代码中提取的值,以便您知道请求是否失败。返回的值通常采用JSON
格式。
在这个答案中有很多关键词可以引导你找到AJAX。我希望这能有所帮助!
使用类似jquery 的ajax
$('form').submit(function(event) {
event.preventDefault();
$.ajax({
url:'index.php',
data:{submitRadio:[radiovalue]},
type:'POST',
success:function(response) {
/* write your code for what happens when the form submit */
}
});
});