2013-02-06 30 views
2

我一種形式,它具有一定的文本字段上工作,下面輸入元素:jQuery驗證並接受=「圖像/ *」

<input class='ignore' name='photo' id='photo' type='file' accept="image/*"/> 

另外,我有腳本:jQuery的,jQueryUI的,jQuery驗證, jQuery不顯眼和不顯眼的ajax。我認爲這是最常見的一組jQuery腳本。

但是我遇到了非常痛苦的問題。在我選擇圖像後,此輸入始終標記爲錯誤。所有瀏覽器都是如此。

我嘗試添加

$("#application-form").validate({ 
    ignore: ".ignore" 
}) 

但這並沒有幫助。 所以,我真的想知道如何處理這個問題...

PS。我也嘗試過使用某種變通方法,通過添加的,而不是輸入元素此腳本:

$("#photo-container").html("<input class='ignore' name='photo' id='photo' type='file' accept='image/*'/> "); 

,但得到完全相同的行爲

+0

你的jQuery沒什麼問題,因爲它代表着。 – Nick

回答

5

解決了!

$("#photo").rules("add", { 
     accept: "jpg|jpeg|png|ico|bmp" 
    }); 
0

這對我而言效果很好。

$("#application-form").validate({ 
    rules: { 
     photo: { 
      required: true, 
      extension: "jpg|jpeg|png|ico|bmp" 
     } 
    }, 
    messages: { 
     photo: { 
      required: "Please upload file.", 
      extension: "Please upload file in these format only (jpg, jpeg, png, ico, bmp)." 
     } 
    }, 
    errorElement: "span", 
    errorPlacement: function (error, element) { 
     error.addClass("help-block"); 
     if (element.prop("type") === "checkbox") { 
      error.insertAfter(element.parent("label")); 
     } else { 
      error.insertAfter(element); 
     } 
    }, 
    submitHandler: function() { 
     //do your stuff here 
    } 
}); 

注意:您必須包括additional-methods.js因爲在覈心驗證插件不包括extension方法。

<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.js"></script>