JavaScript:将函数(this)更改为函数(this.id)

时间:2014-05-31 12:28:56

标签: javascript

我有用于文本框的开放kcfinder文件管理器网址的javascript功能。

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:

<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)

现在我需要将(this)更改为(this.id)以更改type的更改url。即:textbox/input id="video"然后type=videotextbox/input id="audio"type=audio

如何根据需要更改此功能?

DEMO Kcfinder Textbox

1 个答案:

答案 0 :(得分:1)

你可以这样做:

<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>

或者,如果您对ids有所了解,也可以

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"
);

您可能还对MDN's documentation of the HTMLInputElement感兴趣,该{{3}}描述了您在this中使用field的属性。