问题描述
我有一个HTML文件上传页面,我需要禁用提交"按钮,直到选择了文件.然后,提交按钮将被启用,用户可以上传.我假设我可以使用jQuery代码查询文件上载字段的值,以查看是否已选择文件上载.我想我可以检查每当焦点离开文件上传字段时.我担心的是某些浏览器对此可能会表现得有些滑稽,然后我的用户无法上传任何文件.
I have an HTML file upload page and I need the submit button to be disabled until a file is selected. Then the submit button will be enabled and the user can upload. I assume I can use jQuery code to query the file upload field's value to see whether a file has been selected to upload. And I assume I can check that whenever focus has left the file upload field. My concern is that some browsers might act funny with this and then my users can't upload any files.
是否有一种安全的方法(最好是使用jQuery)来避免某些用户无法上传的危险?
Is there a safe way to do this, preferably using jQuery, where I won't be in danger of having some users unable to upload?
推荐答案
此jQuery应该可以使用,并且与文件输入的 change()
事件.该代码还应用了disabled
属性,因此,如果用户禁用了JavaScript,则不应阻止使用该表单:
This jQuery should work, and is tied to the file input's change()
event. This code also applies the disabled
attribute so, in the event the user has JavaScript disabled, it shouldn't prevent the form being used:
$(document).ready(
function(){
$('input:submit').attr('disabled',true);
$('input:file').change(
function(){
if ($(this).val()){
$('input:submit').removeAttr('disabled');
}
else {
$('input:submit').attr('disabled',true);
}
});
});
这篇关于如何禁用提交按钮,直到选择文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!