jQuery 获取输入类型文件的值,如果为空则发出警报

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3292658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:10:56  来源:igfitidea点击:

get the value of input type file , and alert if empty

jquery

提问by Mac Taylor

how to alert if input type file is empty !?

如果输入类型文件为空,如何发出警报!?

this is my jquery code but i didnt know how to get the value

这是我的 jquery 代码,但我不知道如何获取值

$('.upload').live("click",function()
{
    var imgVal = $('#uploadImage').val();
    if(imgVal=='')
    {
        alert("empty input file");

    }
    return false;

});


<input type="file" name="image" id="uploadImage" size="30" />
<input type="submit" name="upload"  class="send_upload" value="upload" />

回答by Fosco

<script type="text/javascript">
$(document).ready(function() {
    $('#upload').bind("click",function() 
    { 
        var imgVal = $('#uploadImage').val(); 
        if(imgVal=='') 
        { 
            alert("empty input file"); 

        } 
        return false; 

    }); 
});
</script> 

<input type="file" name="image" id="uploadImage" size="30" /> 
<input type="submit" name="upload" id="upload"  class="send_upload" value="upload" /> 

回答by Denis

There should be

应该有

$('.send_upload')

but not $('.upload')

但不是 $('.upload')

回答by loyola

HTML Code

HTML代码

<input type="file" name="image" id="uploadImage" size="30" />
<input type="submit" name="upload"  class="send_upload" value="upload" />

jQuery Code using bindmethod

使用绑定方法的jQuery代码

$(document).ready(function() {
    $('#upload').bind("click",function() 
    { if(!$('#uploadImage').val()){
                alert("empty");
                return false;} });  });