zoukankan      html  css  js  c++  java
  • [JavaScript] 表单验证不通过不提交的JS写法


    主要是本世纪初的写法.
    <script> function validateForm(f) { if (f.name.value == "") { alert("You must supply a name"); return false; } return true; } </script> <form id="inputForm" method="post" onsubmit="return validateForm(this)">

    Example 2: event handler Javascript

    (Yep, early 21st century Javascript was pretty lame, too).

    One unifying characteristic that these early Javascript experiments had was that the Javascript was tightly intertwined with the HTML. Buttons had onclickonmouseoveronmouseout, etc. handlers whose code was either in the <head> section of the HTML page if you were lucky, or included just before the code that used it if you were less lucky. What ended up happening as a result was that code ended up being cut and pasted and slightly modified into different contexts. As Javascript got to be more and more complex, the desire for reusable components was greater, and Javascript implementations started supporting more of what was called "unobtrusive" Javascript. An unobtrusive form handler would have been implemented as:

    <script>
    window.onload = function()	{
    	var f = document.getElementById("inputForm");
    	f.onsubmit = function(f)	{
    		if (f.name.value == "")	{
    			alert("You must supply a name");
    			return false;
    		}
    		return true;
    	}
    }
    </script>
    Example 3: Unobtrusive Javascript form validation
    ====================

     
    宛如智障,暗藏锋芒
  • 相关阅读:
    秦腾与教学评估【前缀和+二分】
    c++中成员函数声明时const得作用
    分形【递归】
    飞行兄弟【二进制枚举+异或】
    爬取4k图片网图片
    爬虫爬取博客园文章的文字【练手】
    【YBTOJ】求 f 函数
    【YBTOJ】划分数列
    【学习笔记】高斯消元法
    【Luogu P4588】 [TJOI2018]数学计算
  • 原文地址:https://www.cnblogs.com/zienzir/p/9319824.html
Copyright © 2011-2022 走看看