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

时间:2015-03-26 18:36:23

标签: javascript jquery

我已经尝试了一些方法来实现这一点,但它只是不起作用。

我从这开始:

$('#display_att').click(function() {
        var selected_attribute = $('#first_select2 option:selected').text();
        var attr_value = $('#second_select2 option:selected').text();

        goMap();
   });

我需要的是变量selected_attributeattr_value在函数goMap中可用。

我尝试使用全局变量,但这似乎不起作用(假设我做得正确)。

1 个答案:

答案 0 :(得分:1)

首先,您需要构建goMap来处理额外的“参数”:

function goMap (selected_attribute, attr_value) {
    //do things here
}

然后,在您的点击事件中,将这些变量发送到变量:

$('#display_att').click(function() {
    var selected_attribute = $('#first_select2 option:selected').text();
    var attr_value = $('#second_select2 option:selected').text();

    goMap(selected_attribute, attr_value);
});