簡體   English   中英

使用jQuery判斷文件輸入字段是否為空

[英]Using jQuery to tell if file input field is empty

我有一個帶文件輸入字段和兩個文本輸入字段的PHP表單。 我想要做的是使用jQuery檢查文件輸入字段是否為空,如果不是,則顯示特定的div,但是以下代碼不起作用:

<input type="file" name="blog-entry-image" id="blog-entry-image" />

var fileInput = $.trim($("#blog-entry-image").val());
if (fileInput.length > 0) {
        $("#new-blogentry").click(function() {
        $("#sending").show();
        return true;
    }); 
}

“ new-blogentry”是“提交”按鈕,“ sending”是具有動畫gif的div。

fileInput.length > 0更改為

fileInput != "" && fileInput.trim() != ""
<input type="file" name="blog-entry-image" id="blog-entry-image" />

$(document).ready(function (){
    $("#blog-entry").click(function() {
        var fileInput = $.trim($("#blog-entry-image").val());
        if (fileInput && fileInput !== '') {      
                $("#sending").show();
                return true;    
        }
  });
});

你事情倒退了。 用戶單擊后,需要獲取文件輸入值。

$('#new-blogentry').click(function() {
    var fileInput = $.trim($("#blog-entry-image").val());
    if (fileinput.length > 0) {
        ...
    }
});
if($('#new-blogentry').val()==''))

足以檢查文件輸入是否為空

我建議您將jquery驗證程序插件用於所有基於前端的驗證http://jqueryvalidation.org/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM