将对象变量传递给onchange事件JS

时间:2016-06-05 19:12:38

标签: javascript function events scope socket.io

我现在一直在浏览这个问题的答案,但是还没有找到解决方案。我需要将一个对象传递给一个被触发的函数" onChange"当用户选择选择选项时。目前的代码是:

selecter.onchange = function(){
        var runScript = $("#actionSel option:selected").attr('script');
        console.log(runScript);
        eval("("+runScript+")();");
    }

这里的目的是在"脚本中存储一个函数"选项的attr,然后在选择该选项时运行。但是,对于我的一个函数,我需要从父作用域传递一个变量,以便通过websockets与服务器进行交互。

存储在"脚本中的功能"属性是:

function(){ popConfirm("Restore User","Do you really want to restore the selected users? This will un-delete the selected deleted users.",function(r){ if(r)restoreUser(r,io); });

基本上,这会向用户验证他们想要做他们已经选择的内容,然后将结果传递给我的恢复用户功能。它还需要通过" io"对象。但是,我收到的错误表明io未定义。

任何想法都会非常有帮助。谢谢!

根据要求,这里有一些额外的相关代码片段,显示了IO的引入位置。

AdminIO = new io(servPath);
AdminIO.on('send_users',function(rows){
     toggleLoad();
     appendUsers(rows,AdminIO);       
});

在appendUsers中还有另一个编译选择列表及其选项的函数dropActions(),其中引入了selector.onchange和我之前发布的其他文件。 selector.onchange是创建下拉列表的函数的一部分。函数(){popConfirm()}被添加为在选择该项时运行的函数。构建列表的功能是:

dropActions = function(bActions, lActions, options){
    // bActions = {id: myID, text: "this is my action", elem: document.getElementById('getDiv'), action: function(){ /*mycode here */}}
    // lActions = {text: "select me to run an action", action: function(){ /*mycode here */}}
    bActions = bActions || null;
    lActions = lActions || null;
    options = options || {elem: document.body, id: null};
    if(!bActions && !lActions){ console.error("No actions added or available."); return; }

    var 
        selID = options.id,
        starter = (selID) ? document.getElementById(selID) : options.elem,
        optsBar = document.createElement("header"),
        selecter = document.createElement("select");
        starterSel = document.createElement("option");
    optsBar.id = "actionSelH";
    starterSel.innerText = "More Actions";
    starterSel.setAttribute('script','javascript:void(0)');
    selecter.id = "actionSel";
    selecter.appendChild(starterSel);
    for(var i= 0; bActions.length > i; i++){
        var 
            buttonText = bActions[i].text,
            buttonID = bActions[i].id || 'ACT'+i,
            buttonAction = bActions[i].action,
            button = document.createElement('div');
       button.id = buttonID;
       button.classList.add("actionButton");
       button.innerText = buttonText;
       button.onclick = buttonAction;      
       optsBar.appendChild(button);
    }
    for(var i= 0; lActions.length > i; i++){
        var selText = lActions[i].text,
            selScript = lActions[i].action,
            option = document.createElement('option');
        option.innerText = selText;
        option.setAttribute('script',selScript);
        selecter.appendChild(option);
    }
    selecter.onchange = function(){
        var runScript = $("#actionSel option:selected").attr('script');
        console.log(runScript);
        eval("("+runScript+")();");
    }
    optsBar.appendChild(selecter);
    $(optsBar).insertBefore('#user_list_table');
    //$('#user_list_table').after(optsBar);
    //$(starter).prepend(optsBar);  
},

希望更多上下文有帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用function constructor并将参数直接传递给该函数,而不是使用eval

var scriptFunction = new Function(r, io, runScript);
scriptFunction(r, io);

当然,此代码假设rio始终是您要查找的变量。如果不是这样,那么您必须编写自己的逻辑来从父作用域中获取变量,然后将它们传递给scriptFunction

<强>更新

如果您能够更改提供的脚本,您还可以尝试隐式声明变量rio

function(r, io){ popConfirm("Restore User","Do you really want to restore the selected users? This will un-delete the selected deleted users.",function(r, io){ if(r)restoreUser(r,io); });
相关问题