调用函数在数组中的函数

时间:2017-06-13 13:32:50

标签: javascript html

我想用按钮调用功能。 但是这个函数位于另一个函数中,该函数位于数组中。

demo = {
    volej: function(param) {
      (new initChartist).users(param);
    },
    initPickColor: function(){
        $('.pick-class-label').click(function(){
            var new_class = $(this).attr('new-class');  
            var old_class = $('#display-buttons').attr('data-class');
            var display_div = $('#display-buttons');
            if(display_div.length) {
            var display_buttons = display_div.find('.btn');
            display_buttons.removeClass(old_class);
            display_buttons.addClass(new_class);
            display_div.attr('data-class', new_class);
            }
        });
    },

    initChartist: function(){ 
    function users(cas) {
          $.post("getUsersReg.php", {datum: cas}, function(datas) {
            var casy;
            if(cas == "Rok") {
              casy = ['Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
            } else if(cas == "Mesic") {
              casy = ['1. Týždeň', '2. Týždeň', '3. Týždeň', '4. Týždeň', 'Zbytek měsíce'];
            } else if(cas == "Den") {
              casy = ['Pon', 'Út', 'St', 'Čt', 'Pá', 'So', 'Ne'];
            }
    }
        var data = {
          labels: label,
          series: [uzivatelu]
        };
        /*data = {
            labels: [mesice[3], mesice[5]],
            series: [
              [1, 2]
            ]
          };*/

        var options = {
            seriesBarDistance: 10,
            axisX: {
                showGrid: true
            },
            height: "245px"
        };

        var responsiveOptions = [
          ['screen and (max-width: 640px)', {
            seriesBarDistance: 5,
            axisX: {
              labelInterpolationFnc: function (value) {
                return value[0];
              }
            }
          }]
        ];

        Chartist.Bar('#chartActivity', data, options, responsiveOptions);
      });
    }
}
}

我用这段代码调用它:

<button  onClick="demo.initChartist().users('Now');">Date</button>

如何从onClick启动该功能?

提前感谢您的回复!

1 个答案:

答案 0 :(得分:2)

为了根据需要调用函数,您必须更改代码,如此

<div id=block>
  <input type=range />
  
  <!-- added these for comparison -->
  <input type=checkbox />
  <input type=text value="Input text"/>
  <textarea>Textarea</textarea>
  <span>Span</span>
  
</div>

但是,将initChartist: function(){ function users(cas) { $.post("getUsersReg.php", {datum: cas}, function(datas) { var casy; if (cas == "Rok") { casy = ['Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; } else if(cas == "Mesic") { casy = ['1. Týždeň', '2. Týždeň', '3. Týždeň', '4. Týždeň', 'Zbytek měsíce']; } else if(cas == "Den") { casy = ['Pon', 'Út', 'St', 'Čt', 'Pá', 'So', 'Ne']; } } } return { users: users }; } 移至users范围可能会更好吗?