javascript - 提交ajax表单
问题描述
我需要提交此表格才能使用。这是我的代码,但它不想工作。必须使用此链接发送请求,例如https://www.eymedic.form.php。我怎样才能让它正常工作?
<form method="POST" id="form" action="#" class="form">
<div class="field">
<label for="input-kitId" class="label is-size-7 has-text-weight-light has-text-left">Order ID</label>
<div class="field"><div class="control is-expanded">
<input type="number" id="input-kitId" class="home-form__input input is-danger" name="number" value="" placeholder="Order ID">
</div>
</div>
</div>
<div class="field">
<label for="input-firstName" class="label is-size-7 has-text-weight-light has-text-left">Email</label>
<div class="field"><div class="control is-expanded">
<input type="email" id="input-firstName" class="home-form__input input" name="email" value="" placeholder="Email">
</div>
</div>
</div>
<button type="submit" class="button is-midnightBlue">Generate Booking Reference</button>
</form>
</div>
</div>
<script>
$( "submit" ).click(function() {
if(form.val()) {
var msg = $("#form").serialize();
$.ajax({
type: 'POST',
url: 'https://www.harleymedic.co.uk/qrcodes/form.php',
data: msg,
success: function(data) {
console.log(data);
if (data.status === 'OK') {window.location.href='second_page.html'}
else {alert(data.message)};
}
});
};
});
</script>
解决方案
您没有提交元素,请改用表单元素,并且应该阻止提交操作。
$( "form" ).click(function(e) {
e.preventDefault()
var msg = $("#form").serialize();
$.ajax({
type: 'POST',
url: 'https://www.harleymedic.co.uk/qrcodes/form.php',
data: msg,
success: function(data) {
console.log(data);
if (data.status === 'OK') {window.location.href='second_page.html'}
else {alert(data.message)};
}
});
});
推荐阅读
- bash - Bash - 连接到 ssh(未经授权),抓取 MOTD 文件并退出
- react-native - 将本机帖子图像反应到服务器所需的请求部分“文件”不存在
- c++ - 有没有办法在进程内部调整模块的大小,或者将模块扩展为更长的内存?
- python - 将对称矩阵加载到 pandas DataFrame 中,其中文件具有 3 列格式(行、列、数据)
- c++ - 如何正确实现和访问各种自定义算子枚举器?
- javascript - Javascript - 设置浏览器 Cookie
- c++ - C++ 将目录中的所有文件与 ::filesystem 进行比较
- c++ - Vulkan,为什么验证层(以及规范的扩展)禁止管道不写入某些附件?
- amazon-web-services - 如何更改 Amazon AWS S3 存储桶中每个请求的用户“角色”?
- mysql - 获取表格的字符集