简体   繁体   English

如何从jQuery事件函数返回变量值?

[英]How to return variable value from jquery event function?

Can I get value from jquery event function? 我可以从jquery事件函数中获取价值吗?

I want to get something like this: 我想得到这样的东西:

function Init() {
    var my_variable = $some_object.click(function() {
         var my_variable = 'value';
         return my_variable // I need to get the variable value from click event
                            // but I don't know ways to do it
    });

    console.log(my_variable); // I want to have possibility to use the value here
}

No. 没有。

The function that sets the event handler will have finished running before the event handler runs. 设置事件处理程序的函数将在事件处理程序运行之前完成运行。

No, you can't do that. 不,你不能那样做。 You can only capture the value in a variable defined outside the scope of your handler: 您只能在处理程序范围之外定义的变量中捕获值:

function Init() {
    var my_variable = '';

    $some_object.click(function() {
         my_variable = 'value';
    });

    console.log(my_variable); //Prints empty string

    $some_object.trigger('click');

    console.log(my_variable); //Prints 'value'
}

The answer to your question is No!. 您的问题的答案是否定的! You cannot return a value from event handler. 您不能从事件处理程序返回值。 Now next thing which made you curious is how to get some data which comes from the event handler itself. 现在让您感到好奇的下一件事是如何获取来自事件处理程序本身的一些数据。 This is where event propagation comes and passing some data along with that. 这是事件传播的起点,并随之传递一些数据。

Checkout below fiddle which shows how data which was created in event handler of button click, is being passed for any custom purpose. 小提琴下面的Checkout(结帐)显示了如何将在按钮单击事件处理程序中创建的数据用于任何自定义目的。

http://jsfiddle.net/kowmwq9j/ http://jsfiddle.net/kowmwq9j/

Below is simple html and understandable javascript code 以下是简单的html和可理解的javascript代码

<div id='parent'>
    <button id='test'>CLick</button>
</div>

var a=document.getElementById('test');
a.addEventListener('click', function(){
   var str='sample';
   //creating a custom event with name 'build' and passing object as data
   var event = new CustomEvent('build', { 'detail': str });
   //dispatches the event which invokes the affected listeners
   this.dispatchEvent(event);
})

//Listening for 'build' event 
document.getElementById('parent').addEventListener('build', getData, true);

function getData(e){
  alert(e.detail);
}

Links shared which can help one to understand the concept. 共享的链接可以帮助您理解概念。 dispatchEvent & createCustomEvent .Hope that helps to answer your question! dispatchEventcreateCustomEvent。希望可以帮助您回答问题! Added some comments & links which will help those who aren't aware of the functions & behavior used. 添加了一些注释和链接,这些注释和链接将帮助那些不了解所使用功能和行为的人。

If I understand the question maybe you can do something like this... 如果我理解这个问题,也许您可​​以做这样的事情...

Variable = 5 is used within 2 functions...each function does something with it's value... 在2个函数中使用了Variable = 5 ...每个函数都用它的值来做...
the last function uses the totals of the first 2 functions and again does something.. 最后一个函数使用前两个函数的总和,然后再次执行某项操作。
So the point is that u can access the 2 totals variables and use them in the third function... 因此,关键是您可以访问2个total变量并在第三个函数中使用它们...
As long as you set your function variable as global...so don't put VAR in front of it. 只要将函数变量设置为全局变量,就不要将VAR放在它的前面。
If you set var in front of it u are saying that your variable is private for that function... 如果您在其前面设置var,则表示您的变量是该函数的私有变量...

Change the value of the original variable and all the values changes .... 更改原始变量的值,所有值都会更改..

     variable = 5;                    // this is a global variable
     source = $('div#een h2');        // put your source here

    function Init(){
        source.on('click', function(){
          Total1 = variable * 2;

          console.log(Total1);  // It outputs 10
        });
    };

    Init();

    function Init2(){
     source.on('click', function() {
          Total2 = variable * 5;   

          console.log(Total2); // It outputs 25
      });
     };

    Init2();

    function Init3(){
      source.on('click', function() {
          Total3 = Total1 * Total2; // here we use the 2 Totals...

          console.log(Total3); // outputs the Total of both totals... should be 250
      });
     };

    Init3();

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

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