首页 > 解决方案 > 单击时 JQuery on('click') 不起作用

问题描述

所以我目前正在做这个项目,我遇到了这个问题。我尝试查看控制台,但单击按钮时它没有显示任何内容。我想要做的是当我点击按钮时,它会触发文件输入,但到目前为止,当我点击它时,什么也没发生,甚至没有显示错误。(代码基于this中的答案问题:引导表单上传文件布局)你们能帮我找出我做错了什么吗

这些是代码

TabUploadDokumen.html

<script>
$(document).ready(function () {
    /* Some code here */
});

$('#btn_IdentitasKTP/Paspor').on('click', function () {
    $('#file_IdentitasKTP/Paspor').trigger('click')
});

$('#file_IdentitasKTP/Paspor').change(function () {
    var file_name = this.value.replace(/\\/g, '/').replace(/.*\//, '');
    $('#text_IdentitasKTP/Paspor').val(file_name);
});
</script>

<!--Some other code -->    
<div class='form-group'>
    <label class='control-label'>Nama Dokumen<br /><span style='font-weight:normal;'>Silahkan scan/foto dokumen Anda disini</span></label>
    <div class='form-group'>
        <label for="text_IdentitasKTP/Paspor" id="lbl_IdentitasKTP/Paspor" class="control-label">Identitas(KTP/Paspor)</label>
    </div>
    <div class="input-group">
        <input type="file" id="file_IdentitasKTP/Paspor" name="name_file_IdentitasKTP/Paspor" accept="image/jpg,image/jpeg,application/pdf,image/png" style="display: none" />
        <input type="text" class="form-control" id="text_IdentitasKTP/Paspor" readonly />
        <span class="input-group-btn">
            <button class="btn btn-default" type="button" id="btn_IdentitasKTP/Paspor">Upload KTP/Paspor</button>
        </span>
    </div>`
</div>
<!-- Some other code -->

如果有任何帮助,我正在使用 ASP.NET MVC 5 和 Bootstrap 3.00 版和 Jquery 版 jquery 1.11.1 版。

提前致谢

标签: javascriptjqueryhtml

解决方案


您的选择器包含需要像这样转义的正斜杠:

$("#btn_IdentitasKTP\\/Paspor'")

推荐阅读