欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用JavaScript阻止表單提交的兩種方法

 更新時間:2016年08月11日 16:35:41   投稿:daisy  
本文介紹怎樣利用JavaScript來阻止表單提交的兩種方法,分別是return false和使用preventDefault(),有需要的可以參考借鑒,下面一起來看看。

在JavaScript中,阻止表單默認提交行為的方法有兩種,分別是:

(1) return false

示例代碼

<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!");
  return false;
 };
</script>

(2) 使用preventDefault()

在標(biāo)準瀏覽器中,阻止瀏覽器默認行為使用event.preventDefault(),而在IE6~8中,使用returnValue屬性來實現(xiàn)。

示例代碼

<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(); // 兼容標(biāo)準瀏覽器
  window.event.returnValue = false; // 兼容IE6~8
 };
</script>

以上就是利用JavaScript來阻止表單提交的兩種方法的全部內(nèi)容,希望本文的內(nèi)容對大家使用JavaScript有所幫助。

相關(guān)文章

最新評論