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

    <tfoot id='0r2NH'></tfoot>
  • <legend id='0r2NH'><style id='0r2NH'><dir id='0r2NH'><q id='0r2NH'></q></dir></style></legend>

        <bdo id='0r2NH'></bdo><ul id='0r2NH'></ul>
      1. 从 jQuery.get 嵌套函数中获取 var

        时间:2023-09-04

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

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

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

                  <tfoot id='wPWlV'></tfoot>

                • <legend id='wPWlV'><style id='wPWlV'><dir id='wPWlV'><q id='wPWlV'></q></dir></style></legend>
                  本文介绍了从 jQuery.get 嵌套函数中获取 var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                    function getUserHours(tyPe, tarGet){
                      $.get('/activities/search', { 'type': tyPe }, 
                      function(data){   
                       var hourResultData = jQuery.parseJSON(data);
                       var registeredHours = 0; 
                       for (var i in hourResultData.activities){
                        registeredHours += parseFloat(hourResultData.activities[i].hours);
                       }
                       $(tarGet).empty().append(registeredHours);
                       return registeredHours;
                     });
                  
                    }
                  

                  我想要:

                  alert( getUserHours('r', '#reg-hours'));
                  

                  提醒已注册的时间

                  推荐答案

                  只有关闭 AJAX 的异步 GET 才能直接返回数据:

                  Returning data directly will only work if you turn the asynchronous GET for AJAX off:

                  $.ajax({
                      type: 'GET',
                      url: '/activities/search',
                      data: { 'type': tyPe },
                      async : false,
                      success : function() { }
                   });
                  

                  不建议这样做,因为浏览器会一直阻塞直到您的请求完成.相反,您应该继续遵循异步编程模型,使用函数回调:

                  This is not recommended, because the browser will block until you request is finished. Instead you should continue to follow the asynchronous programming model by using function callback:

                  $.ajax({
                      type: 'GET',
                      url: '/activities/search',
                      data: { 'type': tyPe },
                      async : false,
                      success : function() { }
                   });
                  
                  function getUserHours(tyPe, tarGet, callback)
                  {
                      $.get('/activities/search', { 'type': tyPe }, 
                      function(data)
                      {
                          var hourResultData = jQuery.parseJSON(data);
                          var registeredHours = 0; 
                          for (var i in hourResultData.activities){
                              registeredHours += parseFloat(hourResultData.activities[i].hours);
                          }
                          $(tarGet).empty().append(registeredHours);
                          if($.isFunction(callback))
                              callback(registeredHours);
                      });
                  }
                  
                  getUserHours('r', '#reg-hours', function(hours) {
                      alert(hours);
                  });
                  

                  这篇关于从 jQuery.get 嵌套函数中获取 var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从 ajax 调用返回一个值到父函数 下一篇:返回错误和抛出错误的区别

                  相关文章

                  • <bdo id='vWK2z'></bdo><ul id='vWK2z'></ul>

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

                    <tfoot id='vWK2z'></tfoot>

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

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