Ajax提交Form表單的一種方法

待提交的表單

<form id="updatePublicKey" enctype="multipart/form-data">
  <div class="left">
    <ul>
      <li>
        <div class="label">郵箱</div>
        <div class="ui corner labeled disabled input">
          <input type="text" disabled="disabled" value="${user.email}" placeholder="Required Field" />
        </div>
      </li>
      <li>
        <div class="label">用戶名</div>
        <div class="ui corner labeled disabled input">
          <input type="text" disabled="disabled" value="${user.username}" placeholder="Required Field" />
        </div>
      </li>
      <li>
        <div class="label">
        <span>*</span>Sudo密碼</div>
        <div class="ui corner labeled input">
          <input type="password" iplaceholder="" id="password" name="password" />
        </div>
        <p class="italic inputdescription">需包含字母、數字、符號,八位及以上。
        <br />Sudo密碼在設備上切換到管理員用戶時將使用。</p>
        <p class="red" id="sudoNo" style="display: none;">密碼不符合規則</p>
      </li>
      <li>
        <div class="label">
        <span>*</span>Sudo密碼確認</div>
        <div class="ui corner labeled input">
          <input type="password" placeholder="" id="passwordconfirm" name="passwordconfirm" />
        </div>
        <p class="red" id="twiceNo" style="display: none;">兩次密碼輸入不一致</p>
      </li>
    </ul>
  </div>
  <input id="submit" type="button" class="ui teal button uploadsubmit" value="提交" style="border:0;"/>
</form>

ajax提交Form表單

$("#submit").click(function(){
             var $form = $("#updatePublicKey");
             var formData = new FormData($form[0]);
             formData.append("userName", userName);//添加參數數據
             $.ajax({
                 url: updatekey_url,
                 data:formData,
                 type: 'POST',
                 cache: false,
                 contentType: false,
                 processData: false,
                 xhr: function() {
                     myXhr = $.ajaxSettings.xhr();
                     return myXhr;
                 },
                 success: function(json) {
                     // 
                     if (json.status == 0) {
                         location.href = publickey_index_url;
                     } else {
                        alert(json.msg);
                     }
                  
                 }
             });
    });
相關文章
相關標籤/搜索