[英]change button text when input type = file box is filled
我有以下设置:
readonly
文本框中 如果“选择”按钮更改为“上载”,但仅在选择文件时(文本框不为空时),我也希望输入文本。
我曾尝试对文本框使用on-change
和on-input
,但没有成功...任何提示将不胜感激。
以下是我的代码:
html
<div class="horizontal layout fileUploadContainer">
<paper-button class="upload" id=uploadButton on-click="uploadButton">Select</paper-button>
<input type="text" id="fileName" class="fileName" placeholder="File..." on-click="changeButton" on-change="changeButton"></input>
<input type="file" class="fileUpload" id="fileUpload" on-change="newFile">
</div>
的CSS
.fileUpload {
display: none;
}
.fileName {
width: 200px;
padding-left: 10px;
border: 1px solid var(--divider-color);
}
js
uploadButton : function() {
if(this.$.fileName.value == ""){
this.openUpload();
}
else {
}
},
openUpload : function() {
this.$.fileUpload.click();
},
changeButton : function() {
this.$.uploadButton.innerHTML = "submit";
},
newFile : function(e) {
this.$.fileName.value = this.$.fileUpload.value;
},
以下是一种方法,尽管未测试:
$('#file_button').click(function(){
$(this)
.text("Upload")
.attr('type','submit');
});
您可以在文件输入上使用change事件,例如:
newFile: function(e) {
// Change fileName value to selected filename
this.$.fileName.value = e.target.files[0].name;
// Change uploadButton value
this.$.uploadButton.value = 'Upload';
}
另外,如果以上示例未正确绑定,则可以使用隐藏输入代替:
var input = document.createElement('input');
input.type = 'file';
input.onchange = function(e) {
// Change fileName value to selected filename
this.$.fileName.value = e.target.files[0].name;
// Change uploadButton value
this.$.uploadButton.value = 'Upload';
}.bind(this);
input.click();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.