Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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
在使用jQuery添加的元素上单击“不绑定”_Jquery_Binding - Fatal编程技术网

在使用jQuery添加的元素上单击“不绑定”

在使用jQuery添加的元素上单击“不绑定”,jquery,binding,Jquery,Binding,我有一个日历,可以在其中将事件添加到列表中。它的工作原理如下: 我单击save,然后使用jQuery.post将数据保存到DB 要刷新列表,我调用getList(),它返回事件的html列表 我用新列表替换现有列表 问题是,新元素是在页面加载之后添加的,因此没有绑定 我想我对如何使用jQuerybind或live有些困惑 这是我的密码: // New event - Form Submit jQuery('#formNewEvent').bind('submit',function() {

我有一个日历,可以在其中将事件添加到列表中。它的工作原理如下:

  • 我单击save,然后使用jQuery.post将数据保存到DB
  • 要刷新列表,我调用getList(),它返回事件的html列表
  • 我用新列表替换现有列表
  • 问题是,新元素是在页面加载之后添加的,因此没有绑定

    我想我对如何使用jQuery
    bind
    live
    有些困惑 这是我的密码:

    // New event - Form Submit
    jQuery('#formNewEvent').bind('submit',function() {
    
        var formData = jQuery('#formNewEvent').serializeArray();
    
        jQuery.post("/wp-content/plugins/wp-eventcal/eventcal_jquery.php", { instance: 'addNewEvent', formData : formData },
        function(data)
        {
          if(data == 1)
          {
            jQuery.post("/wp-content/plugins/wp-eventcal/eventcal_jquery.php", { instance: 'getEventList' },
            function(list) {
                jQuery('#eventList').html(list);  // List is updated here
            });
          }       
        });
      return false; //Must have this - otherwise the page just goes blank (at least for me)
    }); 
    
    我知道也有类似的问题,但我还没有找到正确的解决方案。

    您只需要使用而不是绑定,这样新添加的元素也可以正确绑定

    jQuery('#formNewEvent').live('submit',function() {
    
        var formData = jQuery('#formNewEvent').serializeArray();
    
        jQuery.post("/wp-content/plugins/wp-eventcal/eventcal_jquery.php", { instance: 'addNewEvent', formData : formData },
        function(data)
        {
          if(data == 1)
          {
            jQuery.post("/wp-content/plugins/wp-eventcal/eventcal_jquery.php", { instance: 'getEventList' },
            function(list) {
                jQuery('#eventList').html(list);  // List is updated here
            });
          }       
        });
    
    });
    
    您只需要使用而不是绑定,这样新添加的元素也可以正确绑定

    jQuery('#formNewEvent').live('submit',function() {
    
        var formData = jQuery('#formNewEvent').serializeArray();
    
        jQuery.post("/wp-content/plugins/wp-eventcal/eventcal_jquery.php", { instance: 'addNewEvent', formData : formData },
        function(data)
        {
          if(data == 1)
          {
            jQuery.post("/wp-content/plugins/wp-eventcal/eventcal_jquery.php", { instance: 'getEventList' },
            function(list) {
                jQuery('#eventList').html(list);  // List is updated here
            });
          }       
        });
    
    });
    

    当你尝试使用
    .live()
    时发生了什么?当你尝试使用
    .live()
    时发生了什么?啊,太简单了!我还必须在最后一个结束标记之前添加
    return false
    。否则整页就不会是空白的。我来找一个方法来做一些令人讨厌的事情,发现有一个更好的方法。堆叠溢出ftw!谢谢!:)啊,太简单了!我还必须在最后一个结束标记之前添加
    return false
    。否则整页就不会是空白的。我来找一个方法来做一些令人讨厌的事情,发现有一个更好的方法。堆叠溢出ftw!谢谢!:)