繁体   English   中英

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

[英]Passing object variable to onchange event JS

我一直在浏览该问题的答案已有一段时间,但没有找到解决方案。 当用户选择选择选项时,我需要将一个对象传递给正在“ onChange”触发的函数。 当前代码是:

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

这里的目的是将功能存储在选项的“脚本”属性中,然后在选择该选项时将运行该功能。 但是,对于我的功能之一,我需要从父作用域传递一个变量,以便通过websocket与服务器交互。

存储在“脚本”属性中的函数是:

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是创建下拉列表的功能的一部分。 function(){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);  
},

希望更多的上下文有帮助!

除了使用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); });

暂无
暂无

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

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