【问题标题】:Appending Form with jQuery [closed]使用 jQuery 附加表单 [关闭]
【发布时间】:2014-12-03 20:53:00
【问题描述】:

我想使用一个按钮来创建一个询问用户输入的表单并将其发送到 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/

【问题讨论】:

标签: javascript php jquery forms


【解决方案1】:

尝试更改此行$("#report").on("submit",function(event){

$(document).on("submit", ".report", function(event){

还有这个$("&lt;form id='report'$("&lt;form class='report' id='report'

出现问题是因为您使用了 id 而不是 class。 id元素只能是一个

【讨论】:

  • 出现问题是因为您使用了 id 而不是 class。 id元素只能是一个
猜你喜欢
  • 2013-04-20
  • 2014-06-01
  • 1970-01-01
  • 2021-12-16
  • 2013-07-20
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多