简体   繁体   English

按钮变量作用域-如何在父作用域中更新变量? jQuery的

[英]button variable scope - how to update variable in parent scope? jQuery

Updated jsFiddle: http://jsfiddle.net/leongaban/NmH97/ 更新的jsFiddle: http : //jsfiddle.net/leongaban/NmH97/

Inside my main function is a button state modifier function and several click functions. 我主要功能的内部是一个按钮状态修改器功能和多个单击功能。

Having a bit of trouble updating the boolean values in the parent function. 在更新父函数中的布尔值时遇到麻烦。 I could solve this by using global variables, but I know I shouldn't need to do that here. 我可以使用全局变量来解决这个问题,但是我知道我不需要在这里这样做。

In the function below, when #toggle_company is clicked I pass the bool company (which is set to true by default) into the setupToggleButtons function. 在下面的函数中,单击#toggle_company时,我将bool公司(默认情况下设置为true) setupToggleButtonssetupToggleButtons函数中。 The current state is then switched, but the original company bool value is unchanged, how would you write this to target and update the variable company in the parent function wireSearchIcons ? 然后切换当前状态,但原始company bool值不变,您将如何在父函数wireSearchIcons编写此代码以定位和更新变量公司?

var wireSearchIcons = function() {

    // Boolean Flags
    var company = true;
    var phone = true;

    var orange_on = '#F37B21'; var orange_off = '#f3cdb1';
    var green_on = '#3DB54A';  var green_off = '#d8e0c3';

    // Other code...

        $("#toggle_company").unbind('click').bind('click', function () {
            setupToggleButtons(company, '#toggle_company path', orange_on, orange_off);
        });

        function setupToggleButtons(bool, path, on, off) {

            var path = $(path);
            if (bool) {
                path.css('fill', off);
                bool = false;
                return bool;
            } else {
                path.css('fill', on);
                bool = true;
                return bool;
            }

            console.log(bool);
        }
}

When you use the variable in the function call, you will be sending the value that the variable contains, not the variable itself. 在函数调用中使用变量时,将发送变量包含的值,而不是变量本身。 Changing the parameter has no effect on the variable where the value came from. 更改参数不会影响值的来源变量。

Javascript doesn't support sending parameter by reference, so just return the value from the function, and assign it back to the variable: Javascript不支持通过引用发送参数,因此只需从函数返回值,然后将其分配回变量即可:

    $("#toggle_company").unbind('click').bind('click', function () {
        company = setupToggleButtons(company, '#toggle_company path', orange_on, orange_off);
    });

    function setupToggleButtons(bool, path, on, off) {

        var path = $(path);
        if (bool) {
            path.css('fill', off);
            bool = false;
        } else {
            path.css('fill', on);
            bool = true;
        }

        return bool;
    }

It doesn't make sense to write a setupToggleButtons function that does so little. 编写这么少的setupToggleButtons函数没有任何意义。 You should inline the code into the click handlers, or create the click handlers within the setup function. 您应将代码内联到单击处理程序中,或在setup函数中创建单击处理程序。

var wireSearchIcons = function() {

    // Flags
    var company = true;
    var phone = true;

    var orange_on = '#F37B21'; var orange_off = '#f3cdb1';
    var green_on = '#3DB54A';  var green_off = '#d8e0c3';

    // Toggle Button States
    $("#toggle_company").unbind('click').bind('click', function () {
        company = !company;
        $('#div_company').css('background', company ? orange_on : orange_off);
    });
    $("#toggle_phone").unbind('click').bind('click', function () {
        phone = !phone;
        $('#div_phone').css('background', phone ? green_on : green_off);
    });

}

wireSearchIcons();

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

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