最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Checking for attachments in JavaScript - Stack Overflow

programmeradmin2浏览0评论

Prior to submitting a form, I would like to check if a file has been attached and pop up a warning message saying that a file needs to be attached if it hasn't been. I was wondering how to acplish this using JavaScript or Prototype or JQuery etc?

Prior to submitting a form, I would like to check if a file has been attached and pop up a warning message saying that a file needs to be attached if it hasn't been. I was wondering how to acplish this using JavaScript or Prototype or JQuery etc?

Share Improve this question edited Dec 29, 2011 at 15:01 Rob W 349k87 gold badges807 silver badges682 bronze badges asked May 10, 2010 at 15:41 pvsk10pvsk10 2613 silver badges6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 15

Assuming you are using an <input type="file"> field, you can simply check if the element's value is a non-empty string:

<form method="POST">
   <input type="file" id="attachment" />
   <input type="button" onClick="checkAttachment();" value="Send" />
</form>

<script type="text/javascript"> 
   function checkAttachment() {
      if (document.getElementById('attachment').value !== '') {
         alert('File Attached');
      }   
      else {
         alert('No File Attached');
      }
   }
</script> 
发布评论

评论列表(0)

  1. 暂无评论