Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将变量传递给另一个函数_Javascript_Jquery_Function_Variables_Global Variables - Fatal编程技术网

Javascript 将变量传递给另一个函数

Javascript 将变量传递给另一个函数,javascript,jquery,function,variables,global-variables,Javascript,Jquery,Function,Variables,Global Variables,我有两个jQUERY函数,我想把一个变量从第一个传递到第二个。从我读到的内容来看,我需要将变量设置为全局变量,但是我读到并试图复制的方法似乎不起作用 $(function() { $("#selectable").selectable({ stop: function() { $(".ui-selected", this).each(function() { var index = $("#selec

我有两个jQUERY函数,我想把一个变量从第一个传递到第二个。从我读到的内容来看,我需要将变量设置为全局变量,但是我读到并试图复制的方法似乎不起作用

    $(function() {
        $("#selectable").selectable({
            stop: function() {
            $(".ui-selected", this).each(function() {
            var index = $("#selectable li").index(this); });}});});

    $(function() {
    $("#slider-range").slider({
        range: true,
        min: 0,
        max: 180,
        values: [0, 180],
            slide: function(event, ui) {
            var result = $("#result").empty();
            var low = (ui.values[0]);
            var high = (ui.values[1]);
            HERE I NEED THE VARIABLE FROM THE ABOVE FUNCTION

            $.post('search.php',{low: (ui.values[0]), high: (ui.values[1]), HERE I NEED VARIABLE FROM THE ABOVE FUNCTION},
            function(data){
            result.append(data);        
            }); 
  • 我试过这样做:
第一种方法-见下图:

设置变量:示例(索引)

检索变量:函数示例(a){index=a}

这件事我不能去上班。。当我试图将索引作为变量包含在$.post中时,函数会中断

第二种方法 不完全了解此方法,但如果完全理解,这似乎是一个解决方案: document.write()-但我似乎不知道如何再次检索它

希望有人能解决这个问题,因为我已经尝试了很多东西,试图将这个相当简单的东西传递给下一个函数


提前谢谢。

如果我能理解

您必须在两个函数的第一个公共作用域中定义一个全局变量。。。请注意,javascript应该查找的范围越多,过程就越耗时

$(function() {

   var globalVar = '';

   $("#selectable").plugin1({callback:function() {
         // you have access to globalVar
   }});

   $("#slider-range").plugin2({callback:function() {
         // you have access to globalVar
   }});
});

嗯,我通常更喜欢使用名称空间,它更优雅,您可以在页面上的任何位置引用变量

我通常使用的设置如下:

var Namespace = (function() {

    return {

            /**
            * Initialize the page. 
            */
            init: function() {

                  //Here you can set Global Variables 
                  //inside the Namespace using "this"
                  this.variable1 = true;
                  this.variable2 = false;

                  //You may call other functions
                  this.setListeners();

            },

            setListeners() {

                  //You may reference the variables using "this"
                  if(this.variable1 == true) {
                      alert(this.variable2);
                  }
            },

            otherFunction() {

                  //When calling functions with callbacks, 
                  //you should set a variable inside the function 
                  //so you may use that in the callbacks to the namespace
                  var self = this;

                  $("#target").click(function() {
                     //Now you can use the variable self in the callbacks
                     alert(self.variable1);

                     //Or you may also use the namespace
                     alert(Namespace.variable1);
                  });
            }
      };
})();

$(document).ready(function(){
         //Here you may call the init function which should fire 
         //everything else you need in the page load
         Namespace.init();
});

//You may also use the variables anywhere else in the page 
//or in other Javascript files by referencing to the namespace

//Variable
Namespace.variable1;
Namespace.variable2;

//Function
Namespace.otherFunction();

这种结构使代码更清晰,更容易被其他脚本引用。

您可以通过多种方式来实现这一点,不推荐使用非范围变量

$(function() {

  $("#selectable").selectable({
    stop: function() {
      $(".ui-selected", this).each(function() {
        index = $("#selectable").index(this);
      });
    }
  });

});

$(function() {

  $("#slider-range").slider({
    slide: function() {
      // Might want to check index is set as is might not have
      // been set yet
      $.post("search.php", {low: x, high: y, index: index}, function() {
        // Do something
      });
    }
  });

});
正在合并两个支持文档的附件并在其中确定变量的范围

$(function() {

  var index;

  $("#selectable").selectable({
    stop: function() {
      $(".ui-selected", this).each(function() {
        index = $("#selectable").index(this);
      });
    }
  });

  $("#slider-range").slider({
    slide: function() {
      // Might want to check index is set as is might not have
      // been set yet
      $.post("search.php", {low: x, high: y, index: index}, function() {
        // Do something
      });
    }
  });

});
或者将其作为数据属性传递

$(function() {

  $("#selectable").selectable({
    stop: function() {
      $(".ui-selected", this).each(function() {
        $("#selectable").data("index", $("#selectable").index(this));
      });
    }
  });

  $("#slider-range").slider({
    slide: function() {
      var index = $("#selectable").data("index");
      // Might want to check index is set as is might not have
      // been set yet
      $.post("search.php", {low: x, high: y, index: index}, function() {
        // Do something
      });
    }
  });

});
您还可以将其包装在对象中并使用实例属性,或者在外壳或匿名函数中对其进行范围限定

详细说明它是如何断裂的会很好。您可能需要检查该值是否与预期值一致,即

$(function() {
  $("#selectable").selectable({
    stop: function() {
      $(".ui-selected", this).each(function() {
        index = $("#selectable li").index(this);

        // Check the index after the selection
        console.log("Selected index " + index);
      });
    }
  });
});

$(function() {
  $("#slider-range").slider({
    range: true,
    min: 0,
    max: 180,
    values: [0, 180],
    slide: function(event, ui) {

      var result = $("#result").empty();
      var low = (ui.values[0]);
      var high = (ui.values[1]);

      // Check the index before the post
      console.log("Slide index " + index);        

      $.post('search.php', {
          low: ui.values[0], 
          high: ui.values[1], 
          index: index
        }, 
        function(data){
          result.append(data);        
        }); 
    }
  });
});

在第一个函数中将自定义属性添加到“#slider range”,在第二个函数中,您可以获得一个自定义属性,您可以声明var param;(从技术上讲,它将是同一个window.param)如果您首先调用第二个函数,该参数将是“underfind”。但是,如果调用第一个函数,在第二个函数之后,您将能够在documentready调用之外获得这个参数。Set index,并将两个doc ready调用合并为一个。Hi Stuart。这是非常有益的,真的很感谢你给了我这么好的回答,它教会了我很多!你好,尼科利尼。是的,我可以看到以其他方式构建它是有意义的,我可能还想更改结构,因为我希望初始化来自任何(更多)变量的更改。谢谢你的贡献!