我怎么知道我的输入类型是否为"file"?选择了内容 [英] How I know if my input type="file" has content selected
问题描述
我有一个简单的输入文件<input type="file" name="file" id="file">
但是我想在发布后验证输入文件是否包含内容.
i have a simple input file <input type="file" name="file" id="file">
but I'm trying to validate after the post if the input file has content.
我尝试使用$("#file").val()
,但是控件的dom总是一样.
I trying with $("#file").val()
but, the dom of the controls is the same always.
推荐答案
您的意思是:帖子后,您的意思是:
What do you mean by: after the post, do you mean:
-点击提交按钮之后但要发布还是提交?
-After clicking on a submit button but before posting or submitting?
-或者您是说,在提交内容之后,内容已经到达服务器了?
-Or you mean after it has been submitted and the content has reached the server?
如果是第一种情况,请确保将false返回到表单,以便它不提交,并且您可以进行验证,例如:
If it is the first case, make sure to return false to the form so it doesn't submit, and you can validate, for example:
<form onsubmit="return false">
或在提交后调用验证函数后尝试将布尔值返回表单:
or try to return a boolean to the form after calling a validation function on submit:
<form onsubmit="return validate()">
然后使用Jquery验证您的数据:
Then using Jquery validate your data:
function validate(){
valid = true;
if($("#file").val() == ''){
// your validation error action
valid = false;
}
return valid //true or false
}
如果发现错误,则返回false,如果数据有效,则返回true. 在了解了它的工作原理之后,最终,所有这些都可以通过Jquery完成:
if you find errors return false, if the data is valid return true. After understanding how it works, ultimately, all of this can be done with Jquery:
HTML:
<form id="fileform" action="nextpage.php">
JS:
$('#fileform').submit(function(){
valid = true;
if($("#file").val() == ''){
// your error validation action
valid = false;
}
return valid
});
Also, if you are checking to see what type of file is being submitted, you can check this post: jQuery - INPUT type=File , Image FileType Validation options?
如果您要做的是之后验证提交的数据,则必须使用服务器脚本语言(如PHP)来编写验证代码.
If what you want to do is validate the data AFTER submitting then you have to code the validation in a server scripting language like PHP.
这篇关于我怎么知道我的输入类型是否为"file"?选择了内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!