[英]Unable to submit form on confirmation using JQuery
I am trying to submit a form using JQuery but it does not seem to be submitting.我正在尝试使用 JQuery 提交表单,但它似乎没有提交。 What am I doing wrong?
我究竟做错了什么? This is my JS code: It does not log the
hello
word.这是我的 JS 代码:它不记录
hello
词。
document.querySelector("form#myForm").addEventListener('submit', validateForm)
function validateForm(e)
{
e.preventDefault();
var values = $('#myForm').serialize();
$.ajax({
url: "<?php echo route('validate') ?>",
method: 'GET',
async:false,
data: values,
success: function(data) {
var data = JSON.parse(data);
var text = data.text;
var image_url = data.image;
swal({
html: "<div style='text-align: left; margin-left: 10px'><strong>Preview</strong></div>",
showCancelButton: true,
confirmButtonText: "Yes",
cancelButtonText: "No",
confirmButtonColor: "#00ff55",
cancelButtonColor: "#999999",
reverseButtons: true,
}).then((result) => {
if (result.value) {
$("#myForm").submit(function(e){
console.log('hello')
});
}
});
}
});
return false;
}
This is the form
opening tag attributes - <form action="{{url('test')}}" method="post" onsubmit="return validateForm(e);" id="myForm" name="a-form">
这是
form
开始标签属性 - <form action="{{url('test')}}" method="post" onsubmit="return validateForm(e);" id="myForm" name="a-form">
<form action="{{url('test')}}" method="post" onsubmit="return validateForm(e);" id="myForm" name="a-form">
Since you don't see your hello
on your console (developer tools), did you also check for any errors.由于您在控制台(开发人员工具)上看不到您的
hello
,您是否还检查了任何错误。 It's most likely not printed due to that.由于这个原因,它很可能没有打印出来。
As for your code, document.querySelector("form#myForm").addEventListener('submit', validateForm)
is basically doing the same thing as onsubmit="return validateForm(e);"
至于您的代码,
document.querySelector("form#myForm").addEventListener('submit', validateForm)
基本上与onsubmit="return validateForm(e);"
做同样的事情. . So it'd actually be doing the request twice.
所以它实际上会做两次请求。 You can remove either one.
您可以删除任何一个。 Also do make sure your route is printed correctly on the
url
.还要确保您的路线正确打印在
url
上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.