不更改html文本框输入
Dose not change html text box input
我使用具有css属性display:none
的<input type="file">
从文件资源管理器中选择一个文件,但我单击了将触发<input type="file">
的<a>
标记。Bellow我的HTML和JavaScript:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
function BrowseFile() {
$('.attachmentlabel').val('spooky');
$('#file-01').click();
var filePath = $('#file-01').val().split(''''),
fileName = filePath[filePath.length - 1];
if (fileName !== '' && fileName !== undefined) {
$('.attachmentlabel').val(fileName);
}
}
</script>
</head>
<body>
<input type="file" class="hidden-attachment" id='file-01'> @*the hidden field*@
<input type="text" name="Attachment" class="attachmentlabel"> @*here I want to show the file name*@
<a class="btn attachmnentbtn browse" onclick="BrowseFile()">Browse</a>
</body>
</html>
我想做的是,如果我点击<a>
,它将调用BrowseFile()
,并且该方法将触发<input type="file" class="hidden-attachment">
上的点击事件
我尝试了上面的代码,但这并没有改变<input type='text'>
的文本内容。我该如何解决这个问题,这个的原因是什么
添加此代码:
$(document).ready(function(){
$("#file-01").change(function () {
$('.attachmentlabel').val($(this).val())
});
});
只需处理文件控件的更改事件,并在文本框中更新即可。
检查以下代码
var repassword = "hello";
var password = "hello";
repassword = "hello1";
password = "hello";
function BrowseFile() {
$('.attachmentlabel').val('spooky');
$('#file-01').click();
var filePath = $('#file-01').val().split(''''),
fileName = filePath[filePath.length - 1];
if (fileName !== '' && fileName !== undefined) {
$('.attachmentlabel').val(fileName);
}
}
$(document).ready(function(){
$("#file-01").change(function () {
$('.attachmentlabel').val($(this).val())
});
});
.hidden-attachment
{
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="file" class="hidden-attachment" id='file-01'><br>
<input type="text" name="Attachment" class="attachmentlabel"><br>
<a class="btn attachmnentbtn browse" onclick="BrowseFile()">Browse</a>
function BrowseFile() {
$('#file-01').click();
$('.attachmentlabel').val('spooky');
$('#file-01').click();
var filePath = $('#file-01').val().split('''');
fileName = filePath[filePath.length - 1];
if (fileName !== '' && fileName !== undefined) {
$('.attachmentlabel').val(fileName);
}
// Add this code in your function
$('.hidden-attachment').change(function(){
var filename = $(this).val();
$('.attachmentlabel').val(filename);
});
}
演示
相关文章:
- 尝试在PHP中回显输入文本时出现未定义的索引错误
- 使用模拟按键在输入框中自动输入文本
- 如何添加类,同时开始在文本字段中输入文本
- JQuery使用相同的功能自动完成各种输入文本
- 如何设置输入文本框jquery的值
- 输入文本框为空时的阻止按钮asp.网络表单
- 基于单选框更新页眉,选中并选择输入文本
- 将输入文本与某个选项的值相匹配并自动选择
- 如何在 JavaScript 中创建输入文本框
- 如何使用纯Javascript观看输入文本框
- 需要使用javascript获取输入文本,然后将其添加到句子中
- php&js-将电子邮件添加到输入文本中
- 动态启用/禁用来自控制器的输入文本
- .val()不返回输入文本
- 从确认框中预填充输入文本框
- 如何使用JavaScript解析输入文本中给定的数据
- 添加/删除/更改输入文本的部分值
- 根据输入文本按元素排序,AngularJS
- 如何将(a*b)两个输入文本值相乘,并在javascript中随文本变化动态显示
- 如何从Tr重复的输入文本中获取值