更改要用作onchange事件的函数

时间:2016-04-18 08:12:57

标签: javascript jquery

我为列表框组织提供了以下javascript功能:

$("#organization").change(function(e) {
//function updateOrganization() {
    organization = $("#organization").val();
    indicator = $("#indicators").val();
    funcid = "fill_chart_average_score";

    //console.log('changed'); 
    $.getJSON('functions/getfunctions.php', {
        "organization":organization,
        "indicator":indicator,
        "funcid":funcid},

    function(dataChart) {

            var chart = AmCharts.makeChart("chartaveragescore", {
            "theme": "light",
            "type": "serial",
            "startDuration": 1,
            "dataProvider": dataChart,
            "rotate": false,
            "categoryField": "organisatie",
              "valueAxes": [ {
                "gridColor": "#FFFFF",
                "gridAlpha": 0.2,
                "dashLength": 0
              } ],
              "gridAboveGraphs": true,
              "startDuration": 1,
              "graphs": [ {
                "balloonText": "[[category]]: <b>[[value]]</b>",
                "fillColorsField": "fillcolor", //Dit veld heb ik meegegeven vanuit SQL functie en bevat de HEX kleurcodes BD
                "fillAlphas": 0.8,
                "lineAlpha": 0.2,
                "type": "column",
                "valueField": "score"
              } ],
              "chartCursor": {
                "categoryBalloonEnabled": false,
                "cursorAlpha": 0,
                "zoomable": false
              },
              "categoryAxis": {
                "gridPosition": "start",
                "gridAlpha": 0,
                "tickPosition": "start",
                "tickLength": 20
              },
              "export": {
                "enabled": true
              }

        },0);

    $('.chart-input').off().on('input change',function() {
        var property = jQuery(this).data('property');
        var target = chart;
        chart.startDuration = 0;

        });
    });
});

这很有效,但我现在想要的是创建一个不基于$("#organization").change(function(e)的函数,只需创建一个类似function updateOrganization()的函数,并在onchange内调用此函数{1}}元素。当我用html更改$("#organization").change(function(e)时,该功能不再有效。

我是一名初学function updateOrganization()学习者:)。

2 个答案:

答案 0 :(得分:1)

你应该在块之外声明你的功能。类似的东西:

function updateOrganization() {
    organization = $("#organization").val();
    indicator = $("#indicators").val();
    funcid = "fill_chart_average_score";

    // ...
}

然后将其用作:

$("#organization").change(updateOrganization);

答案 1 :(得分:1)

我认为您想要在更改下拉列表时调用函数,并且您希望使用select标记的onchange事件来执行此操作。 This可能会对您有所帮助。