简体   繁体   English

jQuery:如何将.live()与.ajaxForm()链接?

[英]jQuery: How do I chain .live() with .ajaxForm()?

I have a form that I pull in via XHR, so it isn't ready when the DOM is initially rendered. 我有一个通过XHR插入的表单,因此在最初呈现DOM时还没有准备好。 Currently I use something like: 目前,我使用类似:

jQuery("[name='myform']").ajaxForm({ /* code here */ });

and it works fine on a stand alone app. 而且在独立的应用程序上也可以正常工作。 The app I'm embedding it in is huge, and I need to be able to bind ajaxForm to myform before myform is available to the DOM. 我将其嵌入的应用程序非常庞大,在myform可用于DOM之前,我需要能够将ajaxForm绑定到myform。 Is there anyway I can combined .live() here in order to have jQuery watch for it when it is brought in via XHR? 无论如何,我可以在这里组合.live()以便jQuery在通过XHR引入时对其进行监视吗?

** EDIT ** **编辑**

here's the actual code that's being executed. 这是正在执行的实际代码。 no javascript errors. 没有JavaScript错误。 It just does a full page submit instead of an XHR update. 它只是提交整页而不是XHR更新。

 jQuery("[name='clip_form2']").live('submit', function(e) { 
      $(this).ajaxSubmit({
        target: '#form_quotes_highlights_part',

        beforeSerialize: function(form, options) {
          alert("In beforeSerialize...");
          if (validate_time_text_highlights()) {
            if ( $tabChanged ) {
              diff(form[0]);
              jQuery('form[name=clip_form2] input[type=submit]').attr('disabled', 'disabled').val("<%= t('labels.please_wait') %>");
              return true;
            }
            else {
              return false;
            }
          }
          return false;
        },

        success: function() {
          jQuery('#form_quotes_highlights_part').fadeIn('slow');
        },

        complete: function() {
          jQuery("#wizard").expose().close();
        }
      });
      $tabChanged = false;

      add_change_listener("form[name=clip_form2]");

      Tabs.validateCancel( $( "button[name=Cancel]", "form[name=clip_form2]" ) );

      $("#clip_quote").NobleCount('#quote_count');
      $("#clip_quote2").NobleCount('#quote2_count');
      $("#clip_attribution").NobleCount('#attribution_count');

      if ( <%= is_defined?( @permitted_clip_read_only ) && @permitted_clip_read_only %> ) {
        jQuery( 'form[name=clip_form2] input' ).attr( "disabled", true );
        jQuery( 'form[name=clip_form2] textarea' ).attr( "disabled", true );
      } 
      e.preventDefault();
  });

Something like this should do the trick: 这样的事情应该可以解决问题:

jQuery("[name='myform']").live('submit', function(e) {
  $(this).ajaxSubmit(/* code here */);
  e.preventDefault();
});

ajaxSubmit() is where the magic happens (where as ajaxForm() just binds a submit handler), here we're just listening for the submit event and calling ajaxSubmit() then. ajaxSubmit()是在魔术发生(其中为ajaxForm()只绑定一个提交处理程序),在这里我们只是监听submit事件和调用ajaxSubmit()即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM