js 在表單提交前進行操作

來源:互聯網
上載者:User

標籤:get   也會   操作   style   false   orm   方法   inf   name   

最近在寫頁面的時候,需要手動寫一些在表單進行提交前的驗證操作,正好看到了2種阻止表單提交的方法,可以進行一些邏輯處理

方法一:使用return false

原生js寫法:<form id="loginForm" name="loginForm" action="login.aspx" method="post"> <button type="submit" value="Submit" id="submit">Submit</button></form><script> var submitBtn = document.getElementById("submit"); submitBtn.onclick = function (event) {  alert("preventDefault!");  return false; };</script>jquery寫法:<script>$("#loginForm").submit(function(){if(條件){    //此處這裡進行邏輯處理}else{    return false;}})</script>//這裡發現的一個問題就是,在用jq寫法的時候,只要不返回false,邏輯處理完也會自動認可表單

方法二:使用preventDefault()

<form name="loginForm" action="login.aspx" method="post"> <button type="submit" value="Submit" id="submit">Submit</button></form><script> var submitBtn = document.getElementById("submit"); submitBtn.onclick = function (event) {  alert("preventDefault!");  var event = event || window.event;  event.preventDefault(); // 相容標準瀏覽器  window.event.returnValue = false; // 相容IE6~8 };</script>

js 在表單提交前進行操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.