4

我正在尝试将变量传递给函数,我知道有很多关于此的主题,并且我几乎尝试了所有建议,但仍然无法使其正常工作,这些是我的尝试:

编辑:没有onclick,一切正常

var filename = file.name;

<button class="btn btn-danger delete" onclick="deleteImage(\''+filename+'\');">

结果: Uncaught SyntaxError: Unexpected token ILLEGAL

<button class="btn btn-danger delete" onclick="deleteImage(&quot;'+type+'&quot;);">

结果(警报): '文件名'

<button class="btn btn-danger delete" onclick="deleteImage('" + filename + "');">

结果: Uncaught SyntaxError: Unexpected token ILLEGAL

<button class="btn btn-danger delete" onclick="deleteImage(" + filename + ");">

导致: Uncaught SyntaxError: Unexpected token }

这是完整的代码(修改,blueimp fileuploader)

    <script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
    <tr class="template-download fade">
        {% if (file.error) { %}
            <td></td>
            <td class="name"><span>{%=file.name%}</span></td>
            <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
            <td class="error" colspan="2"><span class="label label-important">Error</span> {%=file.error%}</td>
        {% } else { 
            // add the image urls to the file inputbox
            var filename = file.name;
            var prev = $("#mFile").val(); 
            $("#mFile").val(prev + file.name + ","); 

            %}
            <td class="preview">{% if (file.thumbnail_url) { %}
                <a href="modules/mod_stern_form_prijsopgave/upload/server/php/files/{%=file.name%}" title="{%=file.name%}" data-gallery="gallery" download="{%=file.name%}"><img src="modules/mod_stern_form_prijsopgave/upload/server/php/files/thumbnail/{%=file.name%}"></a>
            {% } %}</td>
            <td class="name">
                <a href="modules/mod_stern_form_prijsopgave/upload/server/php/files/{%=file.name%}" title="{%=file.name%}" data-gallery="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
            </td>
            <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
            <td colspan="2"></td>
        {% } %}
        <td>
            <button class="btn btn-danger delete" onclick="deleteImage('" + filename + "');" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}"{% if (file.delete_with_credentials) { %} data-xhr-fields='{"withCredentials":true}'{% } %}>
                <i class="icon-trash icon-white"></i>
                <span>Verwijderen</span>
            </button>
        </td>
    </tr>
{% } %}
</script>

和这样的测试:

    function deleteImage(filename) {
      alert(filename);
    }

我究竟做错了什么?谢谢你的建议

4

5 回答 5

3

尝试 onclick 值的模板格式:

onclick="deleteImage('{%=filename%}');"

也试试 file.name

onclick="deleteImage('{%=file.name%}');" 
于 2013-04-11T12:56:15.070 回答
3
<button id="deleteOnClick" class="btn btn-danger delete">...</button>

在javascript中:

document.getElementById("deleteOnClick").onclick = function(){deleteImage(filename);}

编辑:

如果要删除仅由原始文件名值指定的文件:

var deleteByFile = (function (filename){
    return function(){deleteImage(filename);};
}(filename));
document.getElementById("deleteOnClick").onclick = deleteByFile
于 2013-04-11T12:14:01.970 回答
2

您无法从 HTML 访问 JS 变量。你必须这样做:

var filename = 'testing';
document.getElementById('testdiv')
    .setAttribute('onclick', "alert('" + filename + "')");
filename = 'more testing';

这是一个小提琴

于 2013-04-11T12:12:25.977 回答
-1

尝试使用onclick="deleteImage(" + filename + ");"

于 2013-04-11T12:21:48.633 回答
-1

您可以设置一个变量,如 var counter = 1;

无论在哪里需要,您都可以像这样更新它

     onclick="updateCount('+counter+')"
于 2015-04-23T07:47:39.110 回答