<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function()
{
$('button[type="submit"]').on('click',function(e)
{
e.preventDefault();
var submit_value = $(this).val();
jQuery.post
(
);
});
});
</script>
</head>
<body>
<form method="post" action="php page" id="the-form">
Name <input type="text" name="name" > <br/>
Number <input type="text" name="number" > <br/>
<button type="submit" name="Submit">submit</button>
</form>
</body>
</html>
我应该向 jQuery.post 写入什么以将数据从表单发送到 php 页面,我尝试了一些可能性,但是当我单击“提交”时,php 页面响应“无值”。我不需要在这个脚本中更改 php 页面的任何内容。
最佳答案
您的引用资料已关闭
$('button[type="submit"]').on('click',function(e)
这将观察您的提交按钮,而不是表单。所以当你看到这个时
$(this).val();
它正在查看您的提交按钮(没有任何值),而不是您的表单数据。相反,您应该抓取表单并观察 submit
事件,然后 serialize数据
$("#the-form").on( "submit", function(e) {
e.preventDefault();
var data = $(this).serialize();
jQuery.post('your/url.php', data, function(resp) {
//Do something with the response here
});
});
关于javascript - 将数据发布到 php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28966830/