0

我在提交表单后尝试使用 ajax 来获取另一个

$(document).ready(function() 
    { $("#my_form").submit(function(event) 
        { event.preventDefault(); 
            $this = $(this); 
            $.ajax({ 
                type: "POST",
                data: $this.serialize(), 
                success: function(data) 
                { console.log(data); 
                    document.getElementById("my_form").replaceWith(data.form1);
                }, 
                error: function(data) 
                { console.log(data); 
                } 
            }); 
        }); 
});

但是在页面上,html代码作为字符串插入,如何解决?

4

1 回答 1

0

看起来您正在替换您的提交表单,您可以使用 jQuery .html() 函数来替换。

$(document).ready(function() 
    { $("#my_form").submit(function(event) 
        { event.preventDefault(); 
            $this = $(this); 
            $.ajax({ 
                type: "POST",
                data: $this.serialize(), 
                success: function(data) 
                { console.log(data); 
                    //document.getElementById("my_form").replaceWith(data.form1); //remove this.
                   //add below code
                   var parent=$("#my_form").parent();
                   parent.html(data.form1);
                }, 
                error: function(data) 
                { console.log(data); 
                } 
            }); 
        }); 
});
于 2020-02-10T06:19:45.513 回答