表單驗證——input和required衝突解決
阿新 • • 發佈:2018-11-20
1. input和required衝突解決
Question: 使用表單提交驗證必填欄位時,若同時給 input[type="file"] 新增 readonly 以及 required 屬性,則發現,required必填驗證失效,既不會提示必填,也可以成功提交。
解決方法:將 readonly 改為 onfocus="this.blur()"
<input type="text" name="endDate" onfocus="this.blur()" required autocomplete="off">
2. 表單提交檔案/圖片
Question:
解決方法:設定enctype
預設為:enctype="application/x-www-form-urlencoded"
若要表單中有需要上傳檔案的表單項時,則必須設定 enctype:enctype="multipart/form-data"
<form action="addNewPack" method="post" name="file_upload" enctype="multipart/form-data"> <input type="text" name="name" /> <br> <input type="file" name="image" accept="image/*" /> <br> <input type="submit" value="提交" /> </form>