閱讀900 返回首頁    go 阿裏雲 go 技術社區[雲棲]


form 表單提交數據小記

  • form表單傳統的提交方式,會跳轉頁麵,需要做處理才行,可以在form上配置提交方式、提交地址,可以用submit、和button提交
    <form   name= "myform" method = 'post'  action = 'user_login_submit.action' onsubmit = "return checkUser();" >
    其中checkUser返回false為不提交,你可以在checkUser中做數據校驗
    用button提交時
    function checkUser(){
       var result = document.getElementById("userid").value;
       var password = document.getElementById("passid").value;
       if(result == ""  ){
         alert("用戶名不能為空");
         return false;
       }
       if(password == ""  ){
        alert("密碼不能為空");
         return false;
       }
      document.getElementById("formid").submit();  // 獲取form的ID,然後調用它的submit方法
    }

  • 還有一種方法就是用let data = new FormData() 方法,FormData方法裏傳入頁麵form元素,然後用axios post方法將data傳入即可,注意:form裏input必須有name字段(即需要上傳的字段),如果需要額外的字段且不需要input的情況,有兩種解決辦法:1、可以在頁麵中放入input 設屬性hidden:true,2、用data.appen(‘字段名’, 內容),添加到form對象裏
    <form >
      <input name="name"/>
      <input name="age"/>
    </form>
    <script>
      let form = document.getElementById('forms')
      let data = new FormData(form)
      data.append('age', 12)
    </script>

  • input file accept屬性
      <input type="file" name="pic"  accept="image/gif, image/jpeg" />
      <input type="file" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" ref="inputFile" :name="name"  @change="changePicture">
    <input type="file" name="file"   accept="application/pdf">



最後更新:2017-11-09 10:33:50

  上一篇:go  對標穀歌TPU,比特大陸第一代深度學習專用處理器全球首發
  下一篇:go  說說seo中的快速排名原理