本文介绍了TAFFYdb:将选项传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:此代码是骗子.问题的根源在于通过隐藏字符串并假设它会神奇地变成该变量来重新创建变量的名称..我有很多东西要学习.另外,将大量选项传递给函数的更好的方法是使用JSON.

update: This code is bonkers. The root of the issue here is recreating the name of a variable by concating strings and assuming it would then magically turn into that variable.. I had a lot to learn. Also, a much better way to pass an ambiguous number of options into a function is to use JSON.

var availableTimes,
    selected_unit,
    selected_unit_number;

function createTimePicker(machineNumber){
    availableTimes = machineNumber({booked:"no"}).select("time");
    for (i=0;i<availableTimes.length;i++){
        $('<option/>').val(availableTimes[i]).html(availableTimes[i]).appendTo('#signin_start_time');
    }
}

$(function() {
    $('body').on('click', '#cybex_arctrainer_button', function() {
        selected_unit = "cybex_arctrainer";
    });

    $('body').on('change', '#signin_unit_number_selector', function () {
        selected_unit_number = $("#signin_unit_number_selector option:selected").text();
        unit_plus_number = selected_unit+selected_unit_number;
        createTimePicker(unit_plus_number);
    });
});

几件事:数据库正常工作.如果我运行createTimePicker(cybex_arctrainer1),它会很好地填充availableTimes变量.问题似乎在于组合selected_unit+selected_unit_number并将它们传递给createTimePicker.

A few things: The database works. If I run createTimePicker(cybex_arctrainer1) it fills in the availableTimes variable just fine. The problem seems to be with combining selected_unit+selected_unit_number and passing them to createTimePicker.

推荐答案

此处:

machineNumber({booked:"no"})

machineNumber是一个字符串,但是您试图像调用它一样是一个函数.如果您执行类似的操作,则会得到相同的错误

machineNumber is a string, but you're trying to call it as if it is a function. You'll get the same error if you do something like

someVar = 'bob';
someVar();

您说如果您跑步

createTimePicker(cybex_arctrainer1);

,但是您的代码未使用变量 cybex_arctrainer1作为参数调用createTimePicker,而是执行了以下操作:

but your code is not calling createTimePicker with the variable cybex_arctrainer1 as an argument, instead it's doing something more like:

createTimePicker('cybex_arctrainer1');

这篇关于TAFFYdb:将选项传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 15:28