【发布时间】:2017-03-16 09:32:20
【问题描述】:
当我点击上传图标时,我可以选择附件文件,然后我得到,带有“X”删除附件按钮(@extend .icon-cross; in sass)。当我点击 X 删除附件按钮时,我想实现返回默认输入字段文本(上传字段)而不是附加文件名(在这种情况下例如 asdf.txt)
export default function File(sandbox, hub) {
'use strict';
let $file = $('input.input-file-hidden');
/**
* onChange event handler
* @function onChange
* @private
*/
function onChange() {
let $placeholder = $('span.input-file-placeholder', $(this).closest('div.input-file'));
$placeholder.text(
$(this)[0].files[0].name);
$placeholder.parent().addClass('filled');
}
return {
/**
* initialize File
*/
init() {
$file.on('change', onChange);
hub.on('inputfile:init', components => {
components.forEach(function(component) {
$(component).find('input.input-file-hidden').on('change', onChange);
});
});
}
}
}
&.filled {
.input-file-button .icon {
position: relative;
top: -4px;
left: -2px;
font-size: 15px;
@extend .icon-cross;
@include breakpoint(phone) {
font-size: 24px;
}
}
}
<div class="col-xs-12 col-md-2 no-gutter-right">
<div class="form-elements-label">Normal</div>
<div class="input-file input-file--with-icon">
<label class="text-hide" for="f405">Upload field<sup>*</sup></label>
<input class="input-file-hidden" type="file" required data-max-size="10485760" data-max-size-error-text="* File size is too large">
<input class="input-file-elem" type="text" id="f405">
<span class="input-file-placeholder">Upload field<sup>*</sup></span>
<span class="input-file-bg">input background</span>
<span class="input-file-button"><span class="icon icon-download"></span></span>
<div class="input-file-additional-info text-right">
<span class="additional-info-text">* JPG, PNG (max 10Mb)</span>
<span class="error-text"></span>
</div>
</div>
</div>
到目前为止我在尝试什么,在 JS 添加(单击 X 时使用 .val(''),我将值从“文件名,asdf.txt”更改为“未选择文件” 作为将鼠标悬停在输入字段上的标题),但我需要将输入字段名称更改为默认输入字段文本“上传字段”:
let $remove = $('.input-file input-file--with-icon.filled .icon-download');
remove.on("click", function () {
$placeholder.val('');
});
【问题讨论】:
-
我认为您需要在
remove函数中调用$('.input-file').val('');,然后占位符应该会自动更改。此外,您的 jQuery 选择器错误,您缺少input-file--with-icon类的点 (.):$('.input-file.input-file--with-icon')。 -
不幸的是,不起作用,因为不会再次删除输入文件名,而是将文件值从名称 asdf.txt 输入到未选择文件
标签: javascript jquery