简体   繁体   English

JavaScript:将function(this)更改为function(this.id)

[英]JavaScript: change function(this) to function(this.id)

I have This javascript function for open kcfinder file manager url for textbox. 我有此javascript函数,可打开文本框的kcfinder文件管理器url。

JS: JS:

<script type="text/javascript">

function openKCFinder(field) {
    window.KCFinder = {
        callBack: function(url) {
            field.value = url;
            window.KCFinder = null;
        }
    };
    window.open("http://localhost/cms/kc/browse.php?type=video&dir=files/public", "kcfinder_textbox",
        "status=0, toolbar=0, location=0, menubar=0, directories=0, " +
        "resizable=1, scrollbars=0, width=800, height=600"
    );
}

</script>

HTML: HTML:

<input id="video" onclick="openKCFinder(this)" class="form-control" type="text" name="video" placeholder="add video"> // i need to change openKCFinder(this.id)

<input id="audio" onclick="openKCFinder(this)" class="form-control" type="text" name="video" placeholder="add video"> // i need to change openKCFinder(this.id)

now i need to change (this) to (this.id) for change type of open url . 现在我需要将(this)更改为(this.id)以更改打开url type ie: if textbox/input id="video" Then type=video Or if textbox/input id="audio" , type=audio . 即:如果textbox/input id="video"type=video如果textbox/input id="audio" ,则type=audio

How to change this function for my need? 如何根据需要更改此功能?

DEMO Kcfinder Textbox 演示Kcfinder文本框

You can do it this way: 您可以这样操作:

<script type="text/javascript">

function openKCFinder(field) {
    window.KCFinder = {
        callBack: function(url) {
            field.value = url;
            window.KCFinder = null;
        }
    };
    if (field.id === 'video') {
        window.open("http://localhost/cms/kc/browse.php?type=video&dir=files/public", "kcfinder_textbox",
            "status=0, toolbar=0, location=0, menubar=0, directories=0, " +
            "resizable=1, scrollbars=0, width=800, height=600"
        );
    }
    else if (field.id === 'audio') {
        // open something else
    }
    else {
        // handle something unknown
    }
}

</script>

Or if you're sure about the ids, you could also do 或者,如果您确定ID,也可以这样做

window.open("http://localhost/cms/kc/browse.php?type=" + field.id + "&dir=files/public", "kcfinder_textbox",
    "status=0, toolbar=0, location=0, menubar=0, directories=0, " +
    "resizable=1, scrollbars=0, width=800, height=600"
);

You may also be interested in MDN's documentation of the HTMLInputElement , which describes the properties you have with this in field . 您还可能有兴趣在该HTMLInputElement的MDN的文档 ,描述你的属性与thisfield

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

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