国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery輕量級(jí)表單模型驗(yàn)證插件

2019-11-19 12:41:41
字體:
供稿:網(wǎng)友

JQuery插件,輕量級(jí)表單模型驗(yàn)證,供大家參考,具體內(nèi)容如下

附上源碼和Demo段

var validataForm = (function(model) {  model.Key = "[data-required='true']";  model.ElementList = new Array();  model.FunctionDictionary = new Dictionary();  model.ToastrCustom = function (msg) {    alert(msg);  }  model.AddElement = function (name) {    model.ElementList.push(name);  };  model.AddFunction = function (name, func) {    model.FunctionDictionary.add(name, func);  };  model.Validata = function (formName) {    for (var i = 0; i < model.ElementList.length; i++) {      var thisObj = model.ElementList[i];      var str = formName + " " + thisObj + model.Key;      var elements = $(str);      for (var j = 0; j < elements.length; j++) {        var element = elements.eq(j);        var value = element.val();        var elementType = element.data().type;        var func = model.FunctionDictionary.find(elementType);        if (func) {          var result = func(value, element);          if (result.status) {            var errorInfo = result.message;            model.ToastrCustom(errorInfo);            return;          }        }      }    }  };  model.ElementList.push("input");  model.ElementList.push("select");  model.FunctionDictionary.add("required", function (value, element) {    var name = element.data().name;    return {      status: (value === ""),      message: (value === "" && name + "不能為空")    };  });  return model;})(window.validataForm || {});

調(diào)用處

<form id="ValidataForm">  <input data-required="true" data-name="名稱" data-type="required" value="">  <input data-required="true" data-name="昵稱" data-type="hello">  <button id="Send">提交</button></form><script src="~/js/plugs/jquery-3.3.1.js"></script><script src="~/js/Dictionary.js"></script><script src="~/js/ValidataForm.js"></script><script type="text/javascript">  $("#Send").click(function () {    validataForm.Validata("#ValidataForm");  });</script>

Dictionary這個(gè)對(duì)象是抄一個(gè)博主的
代碼附上,內(nèi)置鏈接

/* https://www.cnblogs.com/baiyangyuanzi/p/6689554.html *//*字典 Dictionary類*/function Dictionary() {  this.add = add;  this.datastore = new Array();  this.find = find;  this.remove = remove;  this.count = count;  this.clear = clear;}function add(key, value) {  this.datastore[key] = value;}function find(key) {  return this.datastore[key];}function remove(key) {  delete this.datastore[key];}function count() {  /*var ss = Object.keys(this.datastore).length;  console.log("ssss  "+ss);  return Object.keys(this.datastore).length;*/  /**/  var n = 0;  for (var key in Object.keys(this.datastore)) {    ++n;  }  return n;}function clear() {  for (var key in this.datastore) {    delete this.datastore[key];  }}

萌新初試前端,有寫得不好的地方,望各位前輩,多多指教。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 洪江市| 遂川县| 宣城市| 灌阳县| 集贤县| 永城市| 阿鲁科尔沁旗| 朝阳区| 泽普县| 林西县| 赣榆县| 电白县| 桓台县| 鄢陵县| 赤水市| 天水市| 黄骅市| 晋宁县| 永善县| 武乡县| 湘潭县| 缙云县| 文登市| 东港市| 迭部县| 富裕县| 屯昌县| 息烽县| 天峻县| 双鸭山市| 阿图什市| 明水县| 珲春市| 高阳县| 昌图县| 营山县| 屯留县| 呼伦贝尔市| 凤城市| 塔河县| 泗水县|