ajax()使用serialize()提交form表單

來源:互聯網
上載者:User

標籤:

jQuery的serialize()方法通過序列化表單值,建立URL編碼文本字串,我們就可以選擇一個或多個表單元素,也可以直接選擇form將其序列化,如:

<form id="form">

  <ul class="register_content_right_form_one">

    <li id="register_content_on" style="margin-top: 16px;">
      <input type="text" name="mobileNumber" id="register_one" placeholder="請輸入您的手機號 " style="width:260px;" onfocus="mobileHnit()" onblur="validMobile()" onkeyup="validMobile()">
      <div class="register_username"></div>
    </li>
    <p class="register_phone_number error_msg"></p>
    <li id="register_content_on_te" style="position: relative;" >
      <input type="text" name="phoneCode" placeholder="請輸入驗證碼 " id="register_three" value="" onblur="validPc(),getPhoneCode();" onkeyup="validPc()" onfocus="pcHnit()">
      <span class="register_dis">
        <a href="javascript:;" onclick="getVerifyCode(this);">擷取驗證碼</a>
      </span>
      <div class="register_yanzheng"></div>
    </li>
    <p class="register_phone_number_yanzheng error_msg"></p>
    <li id="register_content_on">
      <span id="dpwd22" onclick="this.style.display=‘none‘;document.getElementById(‘dpwd‘).style.display=‘block‘;document.getElementById(‘dpwd‘).focus().select();">請輸入密碼 </span>
      <input name="loginPassword" type="password" id="dpwd" style="display:none;" onblur="validPassword()" onkeyup="validPassword()" onfocus="passwordHnit()"/>
      <div class="register_passwords"></div>
    </li>
    <p class="register_password error_msg"> </p>
    <li id="register_content_on">
      <span id="dpw22" onclick="this.style.display=‘none‘;document.getElementById(‘dpw‘).style.display=‘block‘;document.getElementById(‘dpw‘).focus().select();">再次輸入密碼 </span>
      <input name="passwordSecond" type="password" id="dpw" style="display:none;" onblur="validPassword2()" onkeyup="validPassword2()" onfocus="password2Hnit()"/>
      <div class="register_passwords"></div>
    </li>
    <p class="register_password_ag error_msg"> </p>
  </ul>
  <ul class="register_content_right_form_two">
    <li style="font-size: 14px;height:35px;height:40px\9;">
      <p id="register_xuanzhuan_btn" class="register_xuanzhuan_btn"> 我有邀請碼
        <span class="register_xuanzhuan">
          <img src="${ctxStatic}/images/zc_311.jpg">
        </span>
      </p>
    </li>
    <li id="register_content_on_two">
      <input type="text" name="invitationCode" value="${invitationCode}" class="register_yaoqing" style="width:260px;">
      <div class="register_yaoqing_img"></div>
    </li>
    <li style="height: 30px;">
      <input type="checkbox" checked="false" id="check">
      <span style="color: #666;font-size: 14px;">我已閱讀並同意 <a style="color: #05b38b;cursor:pointer;" href="/regAgree" target="_blank">《註冊協議》</a></span>
      <div class="register_check"></div>
    </li>
    <p class="register_checkbox" style="height:15px;width: 333px;margin:0 auto;"></p>

    <li id="register_button" readonly style="margin-top: 8px;">
      <p style="cursor: pointer;">馬上註冊</p>
    </li>
  </ul>
</form>

這樣,我們就可以把序列化的值傳給ajax()作為url的參數,便於使用ajax()提交form表單了,而不需要一個一個擷取表單中的值然後傳給ajax(),舉例如下:

function register(){
    $.ajax({
      url:‘${ctx}/register‘,
      data:$(‘#form‘).serialize(),
      type:‘post‘,
      success:function(data){}
    }
  });
}

ajax()使用serialize()提交form表單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.