【问题标题】:Passing checkbox values to function将复选框值传递给函数
【发布时间】:2020-12-20 02:17:49
【问题描述】:

我正在尝试从 .js 中获取所有选定复选框和单选按钮的值,并通过单击按钮将其传递给 .jsx 函数。 .jsx 函数包含 10 个函数。

我想要实现的是我将数组中的字符串作为函数调用。喜欢

[value1, value2, value3....] 到 值1();价值2(); value3();

这是我在 .js 和 .jsx 中的代码:

//---------------------This is the .js part-----------------------------  

$("#button").on('click', function () {
        var csInterface = new CSInterface();
        var selected = new Array();
    
        $("input[type=checkbox]:checked").each(function () {
            selected.push(this.value);
        });
    
        $("input[type=radio]:checked").each(function () {
            selected.push(this.value);
        });
    
        if (selected.length > 0) {
            csInterface.evalScript("secondFunction(" + selected + ")");
        }   
    });

   //---------------------This is the .jsx part----------------------------- 
    
    function secondFunction(selected){
       
    //the functions below in this function have to be executed one after another
    
      function value1{...};
      function value2{...};
      function value3{...};
    };

【问题讨论】:

    标签: javascript


    【解决方案1】:

    不确定这是否是正确的做法,但据我了解,您希望能够根据其参数(数组)从“secondFunction”调用某个函数。

    我想你可以通过将函数处理程序创建为 javascript 对象来做到这一点,如下所示:

    编辑:我编辑了代码,使其看起来更像您提供的内容,因此您更容易理解。 我也替换了 isArray 检查方法。

    const secondFunction = (selectedArray) => {
        // So that function works if we provide a string instead of an array
        selectedArray = Array.isArray(selectedArray) ? selectedArray : [selectedArray];
    
        // Object containing all your functions with function name as key
        const functionsHandler = {
            opt1 : () => {
                console.log('opt1');
            },
            opt2 : () => {
                console.log('opt2');
            },
            opt3 : () => {
                console.log('opt3');
            },
            opt4 : () => {
                console.log('opt4');
            },
            opt5 : () => {
                console.log('opt5');
            }
        };
    
        selectedArray.forEach((selectedItem) => {
            // Check if the function we want is defined in our object
            if(functionsHandler.hasOwnProperty(selectedItem))
                functionsHandler[selectedItem]();
        })
    }
    
    
    $("#button").on('click', function () {
        // var csInterface = new CSInterface();
        var selected = new Array();
    
        $("input[type=checkbox]:checked").each(function () {
            selected.push(this.id);
        });
    
        $("input[type=radio]:checked").each(function () {
            selected.push(this.id);
        });
    
        if (selected.length > 0) {
            // csInterface.evalScript("secondFunction(" + selected + ")");
            secondFunction(selected);
        }   
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <input type="checkbox" id="opt1" name="opt1" class="options" />
    <label for="opt1">opt1</label>
    
    <input type="checkbox" id="opt2" name="opt2" class="options" />
    <label for="opt2">opt2</label>
    
    <input type="checkbox" id="opt3" name="opt3" class="options" />
    <label for="opt3">opt3</label>
    
    <input type="checkbox" id="opt4" name="opt4" class="options" />
    <label for="opt4">opt4</label>
    
    <input type="checkbox" id="opt5" name="opt5" class="options" />
    <label for="opt5">opt5</label>
    
    <button id="button">Click me</button>

    【讨论】:

    • 感谢您的回答。我不完全理解你的代码。只是为了确保我明确表示:我有 10 个复选框。单击按钮后,每个都应该在 jsx 中触发一个函数。通常这很容易通过在 jsx 中有 10 个函数,例如: let opt1 = document.getElementById('opt1');让 opt2 = document.getElementById('opt2'); ... if (opt1.checked) { csInterface.evalScript('opt1()'); } if (opt2.checked) { csInterface.evalScript('opt2()');但我必须在 jsx 中有一个函数,其中包含 10 个要触发的函数。
    • @MrHeisenberg 我用 sn-p 编辑了我的答案,这样你就可以更好地理解我在说什么。如果这仍然不是您想要达到的目标,请随时发表评论。
    • 多么美妙的一段代码。再次感谢!也许你误会了我,或者我不明白了。我需要在另一个文件的另一个函数中执行函数。我再次编辑了我的原始帖子。我看你和我在一起很难。非常感谢您的努力。
    • 我真的不明白你想要做什么,而我的代码却没有?。为了能够使用字符串作为参数(或数组)从“secondFunction”调用 jsx 中的 10 个函数,您需要像我一样将它们放在一个对象中。如果这不是您想要实现的目标,请提供一个更详细的示例,或者也许有人会比我更幸运地帮助您啊哈。
    • 您介意评论您的代码,哪个部分必须放在哪个文件中吗?我今天有点困惑lol。在我写这篇文章时,我记得 Photoshop API 不理解 ES6/箭头函数。必须谷歌这个。
    猜你喜欢
    • 2021-02-05
    • 2015-06-07
    • 1970-01-01
    • 2020-08-03
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    相关资源
    最近更新 更多