Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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刷新,则无法工作_Javascript_Ajax_Wordpress_Jquery - Fatal编程技术网

Javascript .提交文件';如果绑定对象由ajax刷新,则无法工作

Javascript .提交文件';如果绑定对象由ajax刷新,则无法工作,javascript,ajax,wordpress,jquery,Javascript,Ajax,Wordpress,Jquery,因此,当单击Post Comment按钮时,将进行ajax调用,而不是原始表单提交。在我使用ajax的CommentSubmit按钮刷新页面之前,它工作得很好。假设我只刷新包含post和comments以及按钮的div。之后,不会触发ajax,而是使用原始的提交方式 提交表单的javascript如下所示 jQuery('document').ready(function($){ var commentform=$('#commentform'); // find the comment

因此,当单击
Post Comment
按钮时,将进行ajax调用,而不是原始表单提交。在我使用ajax的CommentSubmit按钮刷新页面之前,它工作得很好。假设我只刷新包含post和comments以及按钮的div。之后,不会触发ajax,而是使用原始的提交方式

提交表单的javascript如下所示

jQuery('document').ready(function($){
    var commentform=$('#commentform'); // find the comment form

    commentform.submit(function(){
//  $('#commentform').submit(function(){
我尝试使用
$('#commentform')
而不是无效的变量

在成功加载新帖子的ajax之后,我尝试再次分配commentform变量。那也没用

javascript的一部分,通过ajax加载post

var $ = jQuery.noConflict();

$(document).ready(function() { 
    $(".mhomepage_item").bind("click", function(){ //bind a click event on a class with name = mhomepage_item

    $.ajax({
            type: "POST",
            url: mhomepage.ajax_url,
            data: { 
                action: "get_post", 
                type: $(this).attr('type'), 
                current_post_id: $('#post_container').attr('current_post_id')
                },
            success: function(response) {
                response_from_json = JSON.parse(response);

                $('#content_container').html(response_from_json['html']);
                commentform=$('#commentform');
     }
    });

    // }
});
是否有人可以建议如何使
绑定到表单提交按钮,即使在通过ajax重新加载按钮之后,也可以使该按钮永久性地使用?

,相应地:

您必须绑定一个按钮才能在
success
callback中单击/提交事件

您可以执行以下操作:

success: function(response) {
      response_from_json = JSON.parse(response);

      $('#content_container').html(response_from_json['html']);
      commentform=$('#commentform');

      $('#commentform').bind("submit", function() { ... } );
}
因此:

您必须绑定一个按钮才能在
success
callback中单击/提交事件

您可以执行以下操作:

success: function(response) {
      response_from_json = JSON.parse(response);

      $('#content_container').html(response_from_json['html']);
      commentform=$('#commentform');

      $('#commentform').bind("submit", function() { ... } );
}
尝试事件委托:

$(document).on("submit","#commentform",function(){
    // action to perform after submit intent
});
通过使用委托事件处理程序,您可以轻松地处理动态创建的元素,而无需执行诸如重新绑定事件处理程序之类的操作

您还可以将事件绑定到调用函数时可用的
主体
或最近的容器,以避免在
文档
中冒泡更多级别。您也可以在您的案例中尝试:

jQuery(document).ready(function($){
    $('#content_container').on("submit","#commentform",function(){
        // action to perform after submit intent
    });
});

尝试事件委托:

$(document).on("submit","#commentform",function(){
    // action to perform after submit intent
});
通过使用委托事件处理程序,您可以轻松地处理动态创建的元素,而无需执行诸如重新绑定事件处理程序之类的操作

您还可以将事件绑定到调用函数时可用的
主体
或最近的容器,以避免在
文档
中冒泡更多级别。您也可以在您的案例中尝试:

jQuery(document).ready(function($){
    $('#content_container').on("submit","#commentform",function(){
        // action to perform after submit intent
    });
});

任何与此相关的HTML都将非常有用…任何与此相关的HTML都将非常有用…+1我正准备像那样发布我的答案。请注意:
.on()
需要jQuery 1.7+第二个注意:您可以用
$(“正文”)
或表单最接近的父选择器替换
$(文档)
。@JFK:实际上,
\content\u容器
就足够了。我认为这两个例子都很有效。非常感谢。你能解释一下为什么它以前不起作用吗?我迷路了。@Radek:您的代码使用直接事件处理程序。事件处理程序直接附加到您的
#commentform
,当您调用
$(“#content_container”).html
时,您的DOM对象将被替换为没有事件处理程序的新对象。+1我正准备这样发布我的答案。请注意:
.on()
需要jQuery 1.7+第二个注意:您可以用
$(“正文”)
或表单最接近的父选择器替换
$(文档)
。@JFK:实际上,
\content\u容器
就足够了。我认为这两个例子都很有效。非常感谢。你能解释一下为什么它以前不起作用吗?我迷路了。@Radek:您的代码使用直接事件处理程序。事件处理程序直接附加到您的
#commentform
,当您调用
$(“#content_container').html
时,您的DOM对象将替换为没有事件处理程序的新对象。