【发布时间】:2016-08-02 07:41:52
【问题描述】:
jQuery执行后提交表单
我当前设置表单的方式,用户可以选择多个选项之一,每个选项都由一个具有唯一值的按钮表示。我想要做的是处理表单数据,然后在一切检查完毕时执行动画。我的问题是,如果我使用.preventDefault(),我无法正确提交带有所选按钮值的表单,没有preventDefault(),动画不会总是发生(我可以让它偶尔工作,但事实并非如此确实是一个可以接受的解决方案)。
需要明确的是,所需的行为是使用事件侦听器检测表单提交,在提交时执行动画,并在将通过 POST 选择的按钮的值发送到下一页时重定向页面。
我试过了
使用
$("input").click()使用
$("form").submit()
这是我用来实验的 HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body{
padding:100px;
text-align: center;
}
form{
margin:20px auto;
padding:20px;
border:solid black 2px;
width: 400px;
background-color:red;
}
form input[type='submit']{
display:block;
width:200px;
height:40px;
margin:10px auto;
}
form input[type="submit"]:hover{
background-color:grey;
font-weight:bold;
}
.hidden{
display:none;
}
#sqare{
margin:20px auto;
background-color:black;
width:200px;
height:200px;
}
</style>
<script>
</script>
</head>
<body>
<?php
if(isset($_POST['my_button'])){
echo $_POST['my_button'];
}
?>
<form id="ver_form" method="post" action="">
<input type="submit" value = "Button No. 0" name = "my_button">
<input type="submit" value = "Button No. 1" name = "my_button">
<input type="submit" value = "Button No. 2" name = "my_button">
<input type="submit" value = "Button No. 3" name = "my_button">
</form>
<div id="square"></div>
</body>
</html>
【问题讨论】: