简体   繁体   English

我的JavaScript函数仅触发一次

[英]My javascript function only fires once

I have a pretty basic step by step wizard I created. 我创建了一个非常基本的逐步向导。 When you select an option from a drop down menu on page 1, I load a new page via jQuery ajax. 当您从第1页的下拉菜单中选择一个选项时,我将通过jQuery ajax加载新页面。 If you hit back, it loads the original page again. 如果您回击,它将再次加载原始页面。

However after loading the original page again, my modelSelect() function that loads page 2 stops working. 但是,再次加载原始页面后,加载页面2的modelSelect()函数停止工作。 Doesn't fire at all. 根本不会开火。 I'm not exactly sure what I'm doing wrong. 我不确定自己在做什么错。

I'm hoping someone can see what I'm doing wrong. 我希望有人能看到我在做什么错。 My code is below: 我的代码如下:

 //Collapse panel handling
var group = jQuery('.estimator-container');
jQuery('.tab-click').click(function() {
    group.find('.collapse.in').collapse('hide');
    jQuery(this).parent().toggleClass('active');
});

  /* -------------------------------------- *\
        New form handler
  \* -------------------------------------- */

  function modelSelect(v) {
      jQuery('#page_2, .estimator-container').toggle();
      // Ajax for loading page_2
      jQuery.ajax({
          type: "POST",
          data: v,
          success: function() {
              jQuery('.estimator-app').load(templateUrl + "/page-estimator2.php?p=" + v);
          }
      });
  }


  jQuery('.estimator-panel').on('change', '.select-model', function() {
      var v = jQuery(this).val();
      modelSelect(v);
  }); //end on change function

  // Back and continue handling
  jQuery('.estimator-app').on('click', '.estimator_form_btn_next', function() {
      var backBtn = jQuery('.estimator_form_btn_back');
      var continueBtn = jQuery('.estimator_form_btn_next');
      var firstPage = jQuery('#contact-first-page');
      var lastPage = jQuery('#contact-last-page');

      if (firstPage.is(":visible")) {
          firstPage.toggle();
          lastPage.toggle();
      }
  }); //end continue

  // Go back
  jQuery('.estimator-app').on('click', '.estimator_form_btn_back', function() {
      var backBtn = jQuery('.estimator_form_btn_back');
      var continueBtn = jQuery('.estimator_form_btn_next');
      var firstPage = jQuery('#contact-first-page');
      var lastPage = jQuery('#contact-last-page');

      if (lastPage.is(":visible")) {
          firstPage.toggle();
          lastPage.toggle();
      } else {
          jQuery.ajax({
              type: "POST",
              // data: v,
              success: function() {
                  jQuery('.estimator-app').load(templateUrl + "/estimator-initial.php");
              }
          });
      }

  }); //end continue

Simple delegation. 简单的委派。

Adding jQuery(document).on('change', '.select-model', function(){ on line 29 made it work. 在第29行上添加jQuery(document).on('change', '.select-model', function(){

Change your this line: 更改您的这一行:

jQuery('.estimator-panel').on('change', '.select-model', function() {

to this 对此

jQuery(document).off('change', '.select-model').on('change', '.select-model', function(e){
   e.preventDefault();

because if HTML is loaded after page load OR by some kind of JS, you need to deattach event and then attach back, 因为如果HTML是在页面加载后加载的,或者是通过某种JS加载​​的,则需要先取消附加事件,然后再附加回去,

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

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