使用 jQuery 追加表单
Appending Form with jQuery
我想使用一个按钮创建一个表单,该表单要求用户输入并将其发送到php文件而不刷新,但是在我将表单附加到html文件并尝试提交后,java函数没有运行。
我用来附加表单的函数:
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");
}
};
未运行的函数:
$(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>";
}
})
}
});
})
这是它现在的样子:http://cs20p.site11.com/
尝试更改此行$("#report").on("submit",function(event){
到$(document).on("submit", ".report", function(event){
而这个$("<form id='report'
到$("<form class='report' id='report'
出现问题是因为您使用了 id 而不是类。Id 元素只能是一个
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何使用WCF服务和javascript表单post上传.doc文件
- jQuery动态使用.append()添加输入,但发布表单不是追加数据
- 如何将 json 类型数据追加到新的表单数据
- 使用 Jquery 将表单字段的值追加到表单操作
- 追加到另一页上的表单
- 使用 jQuery 追加表单
- 无法阻止追加在 Jquery UI 对话框上的表单按钮的缺省操作
- AspxAutoDetectCookie支持在表单提交时追加
- 不能将元素追加到表单数据
- 通过jQuery/Javascript追加搜索表单时出现意外标记
- 追加参数'表单id'使用URL
- Greasemonkey脚本,用于在使用AJAX提交时向表单追加文本
- 将需要的输入追加到现有表单中
- 需要迷你表单追加跟踪号码到URL
- 使用纯JavaScript创建、追加和提交表单
- 用表单输入追加URL
- 想提交表单元素,同时通过追加按钮创建字段
- 表单在提交按钮后被追加
- 在粘贴动作重力表单后向元素追加警告信息