doushi4795 2017-07-23 16:21
浏览 232
已采纳

如何为addEventListener的函数添加更多参数? - Javascript

I have a problem when I work with uploadDisplay_SP_reply_'.$statusid.' As you can see these divs has unique id ($statusid) which is auto increment so if I want to display an image inside a specific div in Javascript addEventListener's function the program won't know in which div should it display the image. And here is my problem. In other functions I could add this unique id e.g (replytext_'.$statusid.') in PHP and function showBtnDiv_reply(e) in Javascript where e is $statusid, but in the function completeHandler_reply I can't. The function completeHandler_reply only gets one parameter from the addEventListener which is the event. So how can I add the $statusid to function completeHandler_reply or is there any way I can solve it?

PHP:

if($isFriend == true || $log_username == $u){
        $statuslist .= '<textarea id="replytext_'.$statusid.'" class="replytext" onfocus="showBtnDiv_reply('.$statusid.')" placeholder="Write a comment..."></textarea>';
        $statuslist .= '<div id="uploadDisplay_SP_reply_'.$statusid.'"></div>';
        $statuslist .= '<div id="btns_SP_reply_'.$statusid.'" class="hiddenStuff">';
            $statuslist .= '<button id="replyBtn_'.$statusid.'" class="btn_rply" onclick="replyToStatus('.$statusid.',\''.$u.'\',\'replytext_'.$statusid.'\',this)">Reply</button>';
            $statuslist .= '<img src="images/camera.png" id="triggerBtn_SP_reply" class="triggerBtn" onclick="triggerUpload_reply(event, \'fu_SP_reply\')" width="22" height="22" title="Upload A Photo" />';
        $statuslist .= '</div>';
        $statuslist .= '<div id="standardUpload_reply" class="hiddenStuff">';
            $statuslist .= '<form id="image_SP_reply" enctype="multipart/form-data" method="post">';
            $statuslist .= '<input type="file" name="FileUpload" id="fu_SP_reply" onchange="doUpload_reply(\'fu_SP_reply\','.$statusid.')"/>';
            $statuslist .= '</form>';
        $statuslist .= '</div>';    
        }
    }

Javascript:

I can add the $statusid here as a parameter e

function showBtnDiv_reply(e){
    _("replytext_"+e).style.height = "130px";
    _("btns_SP_reply_"+e).style.display = "block";
}

function doUpload_reply(id,sid){
    var file = _(id).files[0];
    if(file.name == ""){
        return false;       
    }

    if(file.type != "image/jpeg" && file.type != "image/gif" && file.type != "image/png" && file.type != "image/jpg"){
        alert("That file type is not supported.");
        return false;
    }
    _("triggerBtn_SP_reply").style.display = "none";
    _("uploadDisplay_SP_reply_"+sid).innerHTML = '<img src="images/rolling.gif" width="30" height="30">';
    var formdata = new FormData();
    formdata.append("stPic_reply", file);
    var ajax = new XMLHttpRequest();
    ajax.addEventListener("load", completeHandler_reply, false);
    ajax.addEventListener("error", errorHandler_reply, false);
    ajax.addEventListener("abort", abortHandler_reply, false);
    ajax.open("POST", "php_parsers/photo_system.php");
    ajax.send(formdata);    
}

But here I can't add the $statusid as a 2nd parameter next to event

function completeHandler_reply(event){
    var data = event.target.responseText;
    var datArray = data.split("|");
    if(datArray[0] == "upload_complete"){
        hasImage = datArray[1];
        _("uploadDisplay_SP_reply").innerHTML = '<img src="tempUploads/'+datArray[1]+'" class="statusImage" />';
    } else {
        _("uploadDisplay_SP_reply").innerHTML = datArray[0];
        _("triggerBtn_SP_reply").style.display = "block";
    }
}

function errorHandler_reply(event){
    _("uploadDisplay_SP_reply_").innerHTML = "Upload Failed";
    _("triggerBtn_SP_reply").style.display = "block";
}

function abortHandler_reply(event){
    _("uploadDisplay_SP_reply").innerHTML = "Upload Aborted";
    _("triggerBtn_SP_reply").style.display = "block";
}

I've tried it but didn't work for me.

function completeHandler_reply(event,sid){
    _("uploadDisplay_SP_reply_"+sid);
}
  • 写回答

2条回答 默认 最新

  • douba9020 2017-07-23 16:58
    关注

    You could use an anonymouse function within the eventListener and then call the actual function from within that function.

    ajax.addEventListener("load", function(event){
        completeHandler_reply.call( this, arg1,arg2,arg3 );
    }, false );
    
    or 
    
    ajax.addEventListener("load", function(event){
        completeHandler_reply.apply( this, [ arg1, arg2, arg3 ] );
    }, false );
    

    Passing in whatever arguments are appropriate ~ I used arg1 etc as an example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误