Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ajax响应后jQuery.submit()不起作用_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript ajax响应后jQuery.submit()不起作用

Javascript ajax响应后jQuery.submit()不起作用,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我试图在ajax响应之后调用jQuery.submit()函数。其中ajax响应包含一个表单。但是当我提交表单而不刷新时,它无法调用jQuery.submit()函数 在成功的ajax响应之后,我使用现有代码预先编写表单 success: function(data) { event.preventDefault(); $(".name_wrapper").prepend('<form class="replyName"><textarea na

我试图在ajax响应之后调用jQuery.submit()函数。其中ajax响应包含一个表单。但是当我提交表单而不刷新时,它无法调用jQuery.submit()函数

在成功的ajax响应之后,我使用现有代码预先编写表单

success: function(data) {
        event.preventDefault();
        $(".name_wrapper").prepend('<form class="replyName"><textarea name="name"   placeholder="Write your name"></textarea><button type="submit" class=" btn btn-primary">Reply your name</button></form>');
},
error: function(data) {}

您应该将
submit
事件放在
prepend
表单之后。因为
event
仅在加载
DOM
后绑定到元素

而且由于
prepend
表单是动态的,
jQuery
不知道它必须提交哪个元素,因为它在绑定事件时不存在

success: function(data) {
        event.preventDefault();
        $(".name_wrapper").prepend('<form class="replyName"><textarea name="name"   placeholder="Write your name"></textarea><button type="submit" class=" btn btn-primary">Reply your name</button></form>');

        $( ".replyName").submit(function( event ) {
            alert(event.currentTarget[0].value);

            event.preventDefault();
        });
},
error: function(data) {}
成功:函数(数据){
event.preventDefault();
$(“.name_wrapper”).prepend('Reply your name');
$(“.replyName”).submit(函数(事件){
警报(event.currentTarget[0].value);
event.preventDefault();
});
},
错误:函数(数据){}

由于表单不是在文档中创建的,因此您无法侦听提交事件,除非您在将表单预先提交到dom中后将事件侦听器放入

success: function(data) {
    event.preventDefault();
    $(".name_wrapper").prepend('<form class="replyName"><textarea name="name"   placeholder="Write your name"></textarea><button type="submit" class=" btn btn-primary">Reply your name</button></form>');
    $(".replyName").submit(function(event) {
    alert(event.currentTarget[0].value);
});

},
error: function(data) {}

你可以做两件事:

您可以按如下方式重新绑定单击功能:

success: function(data) {
        event.preventDefault();
        $(".name_wrapper").prepend('<form class="replyName"><textarea name="name"   placeholder="Write your name"></textarea><button type="submit" class=" btn btn-primary">Reply your name</button></form>');
 $('button').bind('click', function (event) {
      event.preventDefault();
      alert(event.currentTarget[0].value);
});
},
error: function(data) {}

在ajax调用的成功函数中添加
submit
事件。该函数可以从立即ajax响应表单调用,也可以从该页面中已经存在的表单调用。您需要再次绑定它,请参阅awnser。您绑定
.submit
事件时,它不存在。请阅读事件委派。
$('body').on('submit', '.replyName', function(e){
  // code here
});
success: function(data) {
        event.preventDefault();
        $(".name_wrapper").prepend('<form class="replyName"><textarea name="name"   placeholder="Write your name"></textarea><button type="submit" class=" btn btn-primary">Reply your name</button></form>');
 $('button').bind('click', function (event) {
      event.preventDefault();
      alert(event.currentTarget[0].value);
});
},
error: function(data) {}
$('button').on('click', function(e){
  // code here
});