2010-07-20 3 views
21

Wie wird gewarnt, wenn die Eingabedatei leer ist?Erhalte den Wert der Eingabedatei und warne, wenn leer

dies mein Jquery-Code ist, aber ich weiß nicht, wie man den Wert

$('.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" /> 

Antwort

41
<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" /> 
+0

nah, das wird nicht funktionieren. aber danke für deinen Versuch –

+1

genau darauf zu achten, ich habe ein ID-Tag auf den Upload-Button und änderte Ihre Auswahl und Funktion verwendet, um den Event-Handler zuzuweisen. – Fosco

+0

@Fosco ist richtig das Problem ist, dass '$ (". Upload ")' wird nicht an Button gebunden, da es 'send_upload' angewendet hat. 'upload' ist seine ID. – TheVillageIdiot

6

Es sollte

$('.send_upload') 

aber nicht $('.upload')

3

HTML-Code

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

jQuery-Code mit binden Methode

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