简体   繁体   English

jQuery在单击时执行两个功能

[英]Jquery execute two functions on click

I am trying to execute functions on click, Below is click button on HTML, 我试图在单击时执行功能,下面是HTML上的单击按钮,

Insights.init() will execute on page load will give me some data from server, now with click on button, i need to pass variable to month function to filter data, and with click i want to execute all functions inside Insights() Insights.init()将在页面加载时执行,这将为我提供服务器上的一些数据,现在单击按钮,我需要将变量传递给month函数以过滤数据,单击以执行Insights()内部的所有函数

  var Insights = function() { var initCheckColor = function(vari) { console.log(vari); } var testFunction = function(vari) { console.log('test'); } return { init: function() { initCheckColor(); testFunction(); } }; }(); jQuery(document).ready(function() { Insights.init(); }); function month(vari) { console.log("hoo"); return { init: function() { initCheckColor(vari); testFunction(); } }; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" onClick="month('test')"> Month </a> 

Now problem is, i can see "hoo" printed on console when i click on link, but i also want to print it with execution of initCheckColor(vari) function, means i want output two times, but i could not output it, 现在的问题是,当我单击链接时,我可以在控制台上看到“ hoo”字样,但是我也想通过执行initCheckColor(vari)函数来打印它,这意味着我想输出两次,但是我无法输出它,

How can i get output two times? 如何获得两次输出?

Problem: Is with this code 问题:带有此代码

function month(vari) {
      console.log("hoo");

      //this block of code
      return {                 
        init: function() {
          initCheckColor(vari);
          testFunction();
        }
      };
      // upto here

    }

When you call the month function you are returning a object with a property named init Note: you are just returning a object and not executing the functions within the property. 调用month函数时,您将返回带有名为init的属性的对象。 注意:您只是返回一个对象,而不在该属性内执行函数。 Also other issue is this property is a function which executes two other function, But those functions are not available in the current scope. 另外一个问题是此属性是一个函数,它执行另外两个函数,但是这些函数在当前范围中不可用。 As they are equal to Private methods for the Insights object. 因为它们等于Insights对象的Private方法。

Solution: Would be to re initialize the object with data just like how you are doing on page load. 解决方案:就像用页面加载一样,用数据重新初始化对象。

I have fixed your code and added comments in the code where the changes were made. 我已经修复了您的代码,并在进行更改的代码中添加了注释。

 var Insights = function() { var initCheckColor = function(vari) { console.log(vari); } var testFunction = function(vari) { console.log('test'); } return { init: function(vari) { // have a input parameter during initialization. initCheckColor(vari); testFunction(); } }; }(); jQuery(document).ready(function() { Insights.init('something'); // I pass in the string "something" now this will be printed by the initCheckColor function. }); function month(vari) { console.log("hoo"); Insights.init(vari); // initialize the Insights object by passing in some value. } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" onClick="month('test')"> Month </a> 

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

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