doushi4795 2017-07-23 08: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 08: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 一部手机能否同时用不同的app进入不同的直播间?
  • ¥15 没输出运行不了什么问题
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部