<small id='HzSZF'></small><noframes id='HzSZF'>

    <i id='HzSZF'><tr id='HzSZF'><dt id='HzSZF'><q id='HzSZF'><span id='HzSZF'><b id='HzSZF'><form id='HzSZF'><ins id='HzSZF'></ins><ul id='HzSZF'></ul><sub id='HzSZF'></sub></form><legend id='HzSZF'></legend><bdo id='HzSZF'><pre id='HzSZF'><center id='HzSZF'></center></pre></bdo></b><th id='HzSZF'></th></span></q></dt></tr></i><div id='HzSZF'><tfoot id='HzSZF'></tfoot><dl id='HzSZF'><fieldset id='HzSZF'></fieldset></dl></div>
    1. <tfoot id='HzSZF'></tfoot>

      <legend id='HzSZF'><style id='HzSZF'><dir id='HzSZF'><q id='HzSZF'></q></dir></style></legend>

          <bdo id='HzSZF'></bdo><ul id='HzSZF'></ul>
      1. jQuery将更多参数传递给回调

        jQuery pass more parameters into callback(jQuery将更多参数传递给回调)
        • <legend id='Vm84r'><style id='Vm84r'><dir id='Vm84r'><q id='Vm84r'></q></dir></style></legend>
            <bdo id='Vm84r'></bdo><ul id='Vm84r'></ul>
              <tbody id='Vm84r'></tbody>
            <tfoot id='Vm84r'></tfoot>

            1. <small id='Vm84r'></small><noframes id='Vm84r'>

                <i id='Vm84r'><tr id='Vm84r'><dt id='Vm84r'><q id='Vm84r'><span id='Vm84r'><b id='Vm84r'><form id='Vm84r'><ins id='Vm84r'></ins><ul id='Vm84r'></ul><sub id='Vm84r'></sub></form><legend id='Vm84r'></legend><bdo id='Vm84r'><pre id='Vm84r'><center id='Vm84r'></center></pre></bdo></b><th id='Vm84r'></th></span></q></dt></tr></i><div id='Vm84r'><tfoot id='Vm84r'></tfoot><dl id='Vm84r'><fieldset id='Vm84r'></fieldset></dl></div>

                  本文介绍了jQuery将更多参数传递给回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有没有办法在 jQuery 中将更多数据传递给回调函数?

                  Is there a way to pass more data into a callback function in jQuery?

                  我有两个函数,我希望回调到 $.post,例如,传递 AJAX 调用的结果数据以及一些自定义参数

                  I have two functions and I want the callback to the $.post, for example, to pass in both the resulting data of the AJAX call, as well as a few custom arguments

                  function clicked() {
                      var myDiv = $("#my-div");
                      // ERROR: Says data not defined
                      $.post("someurl.php",someData,doSomething(data, myDiv),"json"); 
                      // ERROR: Would pass in myDiv as curData (wrong)
                      $.post("someurl.php",someData,doSomething(data, myDiv),"json"); 
                  }
                  
                  function doSomething(curData, curDiv) {
                  
                  }
                  

                  我希望能够将我自己的参数以及从 AJAX 调用返回的结果传递给回调.

                  I want to be able to pass in my own parameters to a callback, as well as the result returned from the AJAX call.

                  推荐答案

                  解决方案是通过闭包来绑定变量.

                  The solution is the binding of variables through closure.

                  作为一个更基本的例子,这里有一个接收和调用回调函数的示例函数,以及一个示例回调函数:

                  As a more basic example, here is an example function that receives and calls a callback function, as well as an example callback function:

                  function callbackReceiver(callback) {
                      callback("Hello World");
                  }
                  
                  function callback(value1, value2) {
                      console.log(value1, value2);
                  }
                  

                  这会调用回调并提供一个参数.现在你想提供一个额外的参数,所以你将回调包装在闭包中.

                  This calls the callback and supplies a single argument. Now you want to supply an additional argument, so you wrap the callback in closure.

                  callbackReceiver(callback);     // "Hello World", undefined
                  callbackReceiver(function(value) {
                      callback(value, "Foo Bar"); // "Hello World", "Foo Bar"
                  });
                  

                  或者,更简单地使用 ES6 箭头函数:

                  Or, more simply using ES6 Arrow Functions:

                  callbackReceiver(value => callback(value, "Foo Bar")); // "Hello World", "Foo Bar"
                  

                  <小时>

                  至于您的具体示例,我没有在 jQuery 中使用 .post 函数,但快速浏览文档表明回调应该是 函数指针 带有以下签名:


                  As for your specific example, I haven't used the .post function in jQuery, but a quick scan of the documentation suggests the call back should be a function pointer with the following signature:

                  function callBack(data, textStatus, jqXHR) {};
                  

                  因此我认为解决方案如下:

                  Therefore I think the solution is as follows:

                  var doSomething = function(extraStuff) {
                      return function(data, textStatus, jqXHR) {
                          // do something with extraStuff
                      };
                  };
                  
                  var clicked = function() {
                      var extraStuff = {
                          myParam1: 'foo',
                          myParam2: 'bar'
                      }; // an object / whatever extra params you wish to pass.
                  
                      $.post("someurl.php", someData, doSomething(extraStuff), "json");
                  };
                  

                  发生了什么?

                  在最后一行中,doSomething(extraStuff)invoked 并且该调用的结果是一个 function pointer.

                  In the last line, doSomething(extraStuff) is invoked and the result of that invocation is a function pointer.

                  因为 extraStuff 作为参数传递给 doSomething 它在 doSomething 函数的范围内.

                  Because extraStuff is passed as an argument to doSomething it is within scope of the doSomething function.

                  extraStuff 在返回的 doSomething 的匿名内部函数中被引用时,它通过闭包绑定到外部函数的 extraStuff 参数.即使在 doSomething 返回之后也是如此.

                  When extraStuff is referenced in the returned anonymous inner function of doSomething it is bound by closure to the outer function's extraStuff argument. This is true even after doSomething has returned.

                  我还没有测试过上述内容,但我在过去 24 小时内编写了非常相似的代码,并且它的工作原理与我所描述的一样.

                  I haven't tested the above, but I've written very similar code in the last 24 hours and it works as I've described.

                  您当然可以传递多个变量而不是单个extraStuff"对象,具体取决于您的个人偏好/编码标准.

                  You can of course pass multiple variables instead of a single 'extraStuff' object depending on your personal preference/coding standards.

                  这篇关于jQuery将更多参数传递给回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Toggle HTML radio button by clicking its label(通过单击标签来切换 HTML 单选按钮)
                  Javascript how to change radio button label text?(Javascript如何更改单选按钮标签文本?)
                  JavaScript radio button confirmation(JavaScript 单选按钮确认)
                  How can I automatically select specific radio buttons with Greasemonkey?(如何使用 Greasemonkey 自动选择特定的单选按钮?)
                  AngularJs. Is it possible to deselect HTML “radio” input by click?(AngularJs.是否可以通过单击取消选择 HTML“收音机输入?)
                  Checking Value of Radio Button Group via JavaScript?(通过 JavaScript 检查单选按钮组的值?)

                  <i id='rnBXj'><tr id='rnBXj'><dt id='rnBXj'><q id='rnBXj'><span id='rnBXj'><b id='rnBXj'><form id='rnBXj'><ins id='rnBXj'></ins><ul id='rnBXj'></ul><sub id='rnBXj'></sub></form><legend id='rnBXj'></legend><bdo id='rnBXj'><pre id='rnBXj'><center id='rnBXj'></center></pre></bdo></b><th id='rnBXj'></th></span></q></dt></tr></i><div id='rnBXj'><tfoot id='rnBXj'></tfoot><dl id='rnBXj'><fieldset id='rnBXj'></fieldset></dl></div>

                      <small id='rnBXj'></small><noframes id='rnBXj'>

                      <tfoot id='rnBXj'></tfoot>
                        <tbody id='rnBXj'></tbody>

                      1. <legend id='rnBXj'><style id='rnBXj'><dir id='rnBXj'><q id='rnBXj'></q></dir></style></legend>

                            <bdo id='rnBXj'></bdo><ul id='rnBXj'></ul>