我目前正在开发一个网站,您可以在其中上传文件:
<input type="file" name="file" id="file" class="form-control" />
然后我有一个按钮,您可以按下该按钮来上传所选文件:
<button class="btn btn-primary" type="submit">Upload CV</button>
在这里,我希望按钮仅在已选择文件的情况下才可单击。使用 JS 可以吗?如何实现?
提前致谢!
最佳答案
使用 Jquery:
$(document).ready(
function(){
$('input:file').change(
function(){
if ($(this).val()) {
$('input:submit').attr('disabled',false);
// or, as has been pointed out elsewhere:
// $('input:submit').removeAttr('disabled');
}
}
);
});
在按钮标签中添加禁用属性。
关于javascript - 检查是否已选择输入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34357504/