简体   繁体   English

使用 js/jquery 将数据值从内部 function 传递到内部 function

[英]passing data values from inner function to inner function with js/jquery

I am combining two functions into a single function and need to pass the value of my id from calla() to callb() so callb() can also use that information in its AJAX call.我将两个函数组合成一个 function 并且需要将我的id的值从 calla( calla()传递到callb()因此callb()也可以在其 AJAX 调用中使用该信息。 When these were two different functions, a button was clicked which triggered my callb() function and passed the id to it.当这是两个不同的函数时,单击一个按钮触发我的callb() function 并将id传递给它。 Now that they are one single function with two calls, I am uncertain how to pass this along.现在他们是一个 function 有两个电话,我不确定如何传递它。

I know it's going to be some type of event listener but when I wrap callb() in a jQuery(document).load( then it tells me callb() is undefined. If I remove callb() then it doesn't run the function at all.我知道这将是某种类型的事件侦听器,但是当我将callb()包装在jQuery(document).load( .load( 中时,它告诉我callb()是未定义的。如果我删除callb()那么它不会运行function。

jQuery(document).on('popup/show', (event, id) => {
  if (id === 123) {
    function callA() {
      jQuery.ajax({
        data: {
          action: 'list_count'
        },
        type: 'post',
        url: my_ajax.ajax_url,
        dataType: 'JSON',
        success: function(data) {
          var id = jQuery().data('id')
          var html = '';
          
          jQuery.each(data, function(key, value) {
            html += '<div class="accordion-header ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-corner-all" id="' + value.id + '" style="display:table-row">';
            html += '<div class="image"  style="display: table-cell";>' + value.img + '</div>';
            html += '<div style="display: table-cell";>' + value.Name + '</div>';
            html += '<div  style="display: table-cell">' + value.Size + '</div>';
            html += '<div  style="display: table-cell"> ' + value.Number + ' </div>';
            html += '<div class="tdid_' + value.id + '"  style="display: table-cell">' + value.Status + '</div>';
            html += '</div>';
            html += '<div id="cid_' + value.id + '" class="bg_clear ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" style="display: table-cell">';
            html += '<div class="bg_clear ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" style="display: table-cell"></div>';
            html += '  </div>';
          });
          jQuery('#list').html(html);
        }
      });
    }

    function callB(id) {
      jQuery.ajax({
        type: 'post',
        url: my_ajax.ajax_url,
        data: {
          action: 'options_function',
          cid: id
        },
        success: function(data) {
          jQuery(data).after('#cid_' + id)
          console.log(data);
          console.log(id);
        }
      });
    }
    
    callA();
    callB();
  }
});

Attempted alternate code for function b尝试使用function b的替代代码

jQuery(document).load(function callB(id) {
  jQuery.ajax({
    type: 'post',
    url: my_ajax.ajax_url,
    data: {
      action: 'options_function',
      cid: id
    },
    success: function(data) {
      jQuery(data).after('#cid_' + id)
      console.log(data);
      console.log(id);
    }
  });
});

callA();
//  callB();

EDIT 1- Add callb into the success of calla编辑 1- 将calla添加到callb的成功中

jQuery(document).on('popup/show', (event, id) => {
  if (id === 123) {
    function callA() {
      jQuery.ajax({
        data: {
          action: 'list_count'
        },
        type: 'post',
        url: my_ajax.ajax_url,
        dataType: 'JSON',
        success: function(data) {
          var id = jQuery().data('id')
          var html = '';
          
          jQuery.each(data, function(key, value) {
            html += '<div class="accordion-header ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-corner-all" id="' + value.id + '" style="display:table-row">';
            html += '<div class="image"  style="display: table-cell";>' + value.img + '</div>';
            html += '<div style="display: table-cell";>' + value.Name + '</div>';
            html += '<div  style="display: table-cell">' + value.Size + '</div>';
            html += '<div  style="display: table-cell"> ' + value.Number + ' </div>';
            html += '<div class="tdid_' + value.id + '"  style="display: table-cell">' + value.Status + '</div>';
            html += '</div>';
            html += '<div id="cid_' + value.id + '" class="bg_clear ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" style="display: table-cell">';
            html += '<div class="bg_clear ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" style="display: table-cell"></div>';
            html += '  </div>';
          });
          jQuery('#list').html(html);
            
    function callB(id) {
      jQuery.ajax({
        type: 'post',
        url: my_ajax.ajax_url,
        data: {
          action: 'options_function',
          cid: id
        },
        success: function(data) {
          jQuery(data).after('#cid_' + id)
          console.log(data);
          console.log(id);
        }
      });
    }
            
            
        }
      });
    }


    
    callA();
   callB();   /* <<<<<<<<<<< tried with this commented out or on, doesnt matter/*
  }
});

Edit 2 - Original button code编辑 2 - 原始按钮代码

<a href="javascript:void(0)" id="options-btn" class="button" onclick="function callB('+ value.id+')" data-id="'+ value.id+'"> </a>

AJAX is asynchronous, so you're calling callB() before the AJAX call in callA() has completed. AJAX 是异步的,因此您在 callA( callA()中的 AJAX 调用完成之前调用callB()

In order to use the id variable in callA() , you need to call callB() from within the callA() success function.为了在 callA( callA()中使用id变量,您需要从 callA( callA()成功 function 中调用callB()

jQuery(document).on('popup/show', (event, id) => {
  if (id === 123) {
    function callA() {
      jQuery.ajax({
        data: {
          action: 'list_count'
        },
        type: 'post',
        url: my_ajax.ajax_url,
        dataType: 'JSON',
        success: function(data) {
          var id = jQuery().data('id')
          var html = '';

          jQuery.each(data, function(key, value) {
            html += '<div class="accordion-header ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-corner-all" id="' + value.id + '" style="display:table-row">';
            html += '<div class="image"  style="display: table-cell";>' + value.img + '</div>';
            html += '<div style="display: table-cell";>' + value.Name + '</div>';
            html += '<div  style="display: table-cell">' + value.Size + '</div>';
            html += '<div  style="display: table-cell"> ' + value.Number + ' </div>';
            html += '<div class="tdid_' + value.id + '"  style="display: table-cell">' + value.Status + '</div>';
            html += '</div>';
            html += '<div id="cid_' + value.id + '" class="bg_clear ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" style="display: table-cell">';
            html += '<div class="bg_clear ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" style="display: table-cell"></div>';
            html += '  </div>';
          });
          jQuery('#list').html(html);
          callB(id);
        }
      });
    }

    function callB(id) {
      jQuery.ajax({
        type: 'post',
        url: my_ajax.ajax_url,
        data: {
          action: 'options_function',
          cid: id
        },
        success: function(data) {
          jQuery(data).after('#cid_' + id)
          console.log(data);
          console.log(id);
        }
      });
    }

    callA();
  }
});

What will make your life easier, is that you can define a function in one place, then call it later from another place.让您的生活更轻松的是,您可以在一个地方定义一个 function,然后稍后从另一个地方调用它。 You don't need to define your functions inside an if block, or even inside your document.load callback.您不需要在if块中定义函数,甚至不需要在document.load回调中定义函数。 You can define your functions at the top-most level of your script.您可以在脚本的最顶层定义函数。

A simplified example:一个简化的例子:

function callA() {
 // ... code ...
}

function callB() {
 // ... code ...
}

jQuery(document).on('popup/show', (event, id) => {
  // you can run callA and/or callB however you want
});

If you want to run callA, which makes an AJAX call, then get some data and pass it to callB, you'll want to use the callback pattern like this:如果您想运行调用 AJAX 的 callA,然后获取一些数据并将其传递给 callB,您将需要像这样使用回调模式:

function callA(callback) {
  jQuery.ajax({
    // ... ajax options ...
    success: function(data) {
      var id = jQuery().data('id');
      // you can pass functions as arguments to other functions! :)
      callback(id);
    }
  });
}

function callB(id) {
  // ... use 'id' here ...
}

jQuery(document).on('popup/show', (event, id) => {
  // pass callB as the callback to callA
  // callB will run after in callA's ajax success method
  callA(callB);
});

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

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