Javascript 用jQuery追加表单
我想使用一个按钮创建一个表单,请求用户输入并将其发送到php文件而不刷新,但是在我将表单附加到html文件并尝试提交它之后,java函数没有运行 我用来附加表单的函数:Javascript 用jQuery追加表单,javascript,php,jquery,forms,Javascript,Php,Jquery,Forms,我想使用一个按钮创建一个表单,请求用户输入并将其发送到php文件而不刷新,但是在我将表单附加到html文件并尝试提交它之后,java函数没有运行 我用来附加表单的函数: function reporterror(){ if (document.getElementById("report")){ console.log("Still have report"); }else{ $("<form id='report' action='report.php' method=
function reporterror(){
if (document.getElementById("report")){
console.log("Still have report");
}else{
$("<form id='report' action='report.php' method='post'><div ><h2 style='text-align:center;'>Report Error</h2><h3>Description:</h3><textarea rows='10' cols='60' name='report' id='description' placeholder='No more than 500 words' style='border-radius:10px;border:1px solid #000;'></textarea><input type='submit' style='border-radius:5px;'></input><input type='button' onclick='reportclose()' value='Close' style='border-radius:5px;'></input></div></form>").appendTo("#content");
}
函数报告错误(){
if(document.getElementById(“报告”)){
console.log(“仍然有报告”);
}否则{
$(“报告错误描述:”).appendTo(#content);
}
})
未运行的函数:
$(document).ready(function () {
$("#report").on("submit",function(event){
event.preventDefault();
if (document.getElementById("description").value==""){
$(".warning").remove();
$("#description").after("<p class='warning' style='color:red'>Description is required</p>");
console.log("yes");
}else{
$.ajax({
type:"POST",
url:"report.php",
data:$("#report").serialize(),
success:function(){
console.log("Great");
document.getElementById("report").innerHTML="<h2>Thank You for helping us to improve!</h2><br><p>Your message have been successfully sented to this awesome website!</p><input type='button' onclick='reportclose()' value='Close' style='border-radius:5px;'></input>";
}
})
}
});
$(文档).ready(函数(){
$(“#报告”)。关于(“提交”,功能(事件){
event.preventDefault();
if(document.getElementById(“description”).value==“”){
$(“.warning”).remove();
$(“#说明”)。之后(说明是必需的
);
控制台日志(“是”);
}否则{
$.ajax({
类型:“POST”,
url:“report.php”,
数据:$(“#报告”).serialize(),
成功:函数(){
console.log(“很棒”);
document.getElementById(“report”).innerHTML=“感谢您帮助我们改进!
您的邮件已成功发送到这个很棒的网站!”;
}
})
}
});
})
这就是它现在的样子:尝试更改此行
$(“#报告”)。在(“提交”,函数(事件){
到$(文档)。在(“提交”、“.report”上,函数(事件){
这是
$(“我想您需要委托Submit事件:但是如何调用reporterror()
方法?”)问题出现了,因为您使用了id而不是class。id元素只能是一个