我怎么知道我的输入类型=“文件”是否选择了内容

时间:2022-05-13 07:31:02

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.

我有一个简单的输入文件但是我试图在帖子后验证输入文件是否有内容。

I trying with $("#file").val() but, the dom of the controls is the same always.

我尝试使用$(“#file”)。val()但是,控件的dom始终是相同的。

3 个解决方案

#1


27  

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?

- 或者你的意思是它已经提交并且内容已经到达服务器了?

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:

如果是第一种情况,请确保将false返回到表单,以便它不提交,并且您可以验证,例如:

<form onsubmit="return false">

or try to return a boolean to the form after calling a validation function on submit:

或者在提交时调用验证函数后尝试向表单返回一个布尔值:

<form onsubmit="return validate()">

Then using Jquery validate your data:

然后使用Jquery验证您的数据:

function validate(){

  valid = true;

     if($("#file").val() == ''){
         // your validation error action
        valid = false;

     }

    return valid //true or false
}

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:

如果发现错误返回false,如果数据有效则返回true。在了解它的工作原理之后,最终所有这些都可以通过Jquery完成:

HTML:

HTML:

<form id="fileform" action="nextpage.php">

JS:

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?

此外,如果您正在检查要提交的文件类型,可以查看以下帖子:jQuery - INPUT type = File,Image FileType验证选项?

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.

如果您想要做的是在提交后验证数据,那么您必须使用PHP等服务器脚本语言对验证进行编码。

#2


4  

try to get the value after the form submission like this

尝试在表单提交后获取此值

$('#yourform').submit(function(){
     var val = $("#file").val();
    if(val == ''){
       return false;
    }
});

#3


3  

trying to validate after the post

试图在帖子后验证

This would be something you would do server-side not on the client via jQuery. If you tag your question with the server-side language you're using we can then provide you with the help you need.

这将是你在服务器端不通过jQuery在客户端做的事情。如果您使用您正在使用的服务器端语言标记问题,我们可以为您提供所需的帮助。

#1


27  

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?

- 或者你的意思是它已经提交并且内容已经到达服务器了?

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:

如果是第一种情况,请确保将false返回到表单,以便它不提交,并且您可以验证,例如:

<form onsubmit="return false">

or try to return a boolean to the form after calling a validation function on submit:

或者在提交时调用验证函数后尝试向表单返回一个布尔值:

<form onsubmit="return validate()">

Then using Jquery validate your data:

然后使用Jquery验证您的数据:

function validate(){

  valid = true;

     if($("#file").val() == ''){
         // your validation error action
        valid = false;

     }

    return valid //true or false
}

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:

如果发现错误返回false,如果数据有效则返回true。在了解它的工作原理之后,最终所有这些都可以通过Jquery完成:

HTML:

HTML:

<form id="fileform" action="nextpage.php">

JS:

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?

此外,如果您正在检查要提交的文件类型,可以查看以下帖子:jQuery - INPUT type = File,Image FileType验证选项?

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.

如果您想要做的是在提交后验证数据,那么您必须使用PHP等服务器脚本语言对验证进行编码。

#2


4  

try to get the value after the form submission like this

尝试在表单提交后获取此值

$('#yourform').submit(function(){
     var val = $("#file").val();
    if(val == ''){
       return false;
    }
});

#3


3  

trying to validate after the post

试图在帖子后验证

This would be something you would do server-side not on the client via jQuery. If you tag your question with the server-side language you're using we can then provide you with the help you need.

这将是你在服务器端不通过jQuery在客户端做的事情。如果您使用您正在使用的服务器端语言标记问题,我们可以为您提供所需的帮助。