hondy.form.js
范本:Hondy.Cloud.Agent.JS.Function.JS
适合表单与登陆系统,组合使用见:
弘帝表单系统设计参考CSS:http://wh.ac.cn/forum.php?mod=viewthread&tid=2834&fromuid=1
- $(function () {
- $(".empty").click(function () { $(this).parent().prev().val(""); });
- });
- textLimitCheck = function (thisArea, maxLength) {
- if (thisArea.value.length > maxLength) {
- alert(maxLength + ' 个字限制. \r超出的将自动去除.');
- thisArea.value = thisArea.value.substring(0, maxLength);
- thisArea.focus();
- }
- wordCount.innerText = thisArea.value.length;
- };
- var file = function (name, title, placeholder, value, regex, help){
- var o = $("form ." + name);
- var maxlength = "";
- var need = "";
- var required = "";
- var readonly = "";
- var pattern = "";
- if (placeholder !== "") { placeholder = " placeholder="" + placeholder + """; }
- if (o.attr("maxlength") !== undefined) { maxlength = " maxlength="" + o.attr("maxlength") + """; }
- if (o.attr("need") !== undefined) {
- if (o.attr("need").indexOf("required") >= 0) { need = "<span class='red'>*</span>"; required = " required"; }
- if (o.attr("need").indexOf("readonly") >= 0) { readonly = " readonly"; }
- }
- if (regex !== "") { pattern = " pattern="" + regex + """; }
- if (value !== "") { value = " value='" + value + "'"; } else { value = ""; }
- o.html("<label class='col-md-2 col-form-label'>" + title + need + "</label><div class='col-md-5'><input type="file" name="" + name + "" class="form-control-file"" + value + placeholder + maxlength + readonly + required + pattern + " /></div><div class='d-flex col-md-5 text-muted align-items-center'>" + help + "</div>");
- };
- var select = function (name, title, tag, options, sel, help) {
- var o = $("form ." + name);
- o.html("<label class='col-md-2 col-form-label'>" + title + "<span class='red'>*</span></label><div class='col-md-10 row align-items-center'><div class="col-auto"><select name="" + tag + "" class="custom-select">" + option(options, sel) + "</select></div><div class='col-auto text-muted'>" + help + "</div></div>");
- };
- var option = function (options, sel) {
- var args = "";
- for (var i in options) {
- if (sel === i) { selected = " selected"; } else { selected = ""; }
- args = args + "<option value="" + options[i][0] + """ + selected + ">" + options[i][1] + "</option>";
- }
- return args;
- };
- var input = function (type, name, title, placeholder, value, regex, help) {
- var o = $("form ." + name);
- var maxlength = "";
- var need = "";
- var required = "";
- var readonly = "";
- var pattern = "";
- if (placeholder !== "") { placeholder = " placeholder="" + placeholder + """; }
- if (o.attr("maxlength") !== undefined) { maxlength = " maxlength="" + o.attr("maxlength") + """; }
- if (o.attr("need") !== undefined) {
- if (o.attr("need").indexOf("required") >= 0) { need = "<span class='red'>*</span>"; required = " required"; }
- if (o.attr("need").indexOf("readonly") >= 0) { readonly = " readonly"; }
- }
- if (regex !== "") { pattern = " pattern="" + regex + """; }
- if (value !== "") { value = " value='" + value + "'"; } else { value = ""; }
- o.html("<label class='col-md-2 col-form-label'>" + title + need + "</label><div class='col-md-5'><input type="" + type + "" name="" + name + "" class="form-control"" + value + placeholder + maxlength + readonly + required + pattern + " /></div><div class='d-flex col-md-5 text-muted align-items-center'>" + help + "</div>");
- };
- var rang = function (name, title, value, input, unit, help) {
- var o = $("form ." + name);
- var need = "";
- var readonly = "";
- var min = "", max = "", step = "";
- if (o.attr("min") !== undefined) { min = " min='" + o.attr("min") + "'"; }
- if (o.attr("max") !== undefined) { max = " max='" + o.attr("max") + "'"; }
- if (o.attr("step") !== undefined) { step = " step='" + o.attr("step") + "'"; }
- if (o.attr("need") !== undefined) {
- if (o.attr("need").indexOf("required") >= 0) { need = "<span class='red'>*</span>"; }
- if (o.attr("need").indexOf("readonly") >= 0) { readonly = " readonly"; }
- }
- if (value !== "") { var val = " value='" + value + "'"; } else { val = ""; }
- o.html("<label class='col-md-2 col-form-label'>" + title + need + "</label><div class='col-md-5 align-items-center'><input type="range" name="" + name + "" class="custom-range"" + val + min + max + step + readonly + " /></div><div class='col-md-5 row align-items-center'><div class='col-auto'><input name='" + input + "' class="form-control" value='" + value + "' size='2' /></div><div class='text-muted'>" + unit + "</div><div class='col-auto text-muted'>" + help + "</div></div>");
- bind("range", name, input);
- };
- //list包括内容:string[]={value,title,memo}
- var radio = function (name, title, list, value, help) {
- var o = $("form ." + name);
- var need = "";
- var readonly = "";
- if (o.attr("need") !== undefined) {
- if (o.attr("need").indexOf("required") >= 0) { need = "<span class='red'>*</span>"; }
- if (o.attr("need").indexOf("readonly") >= 0) { readonly = " readonly"; }
- }
- var content = "";
- var lst = eval(list);
- for (var i in lst) {
- var id = "";
- var val = "";
- var text = "";
- switch (lst[i].length) {
- case 1:
- id = name + i;
- val = " value=" + lst[i];
- text = lst[i];
- break;
- case 2:
- id = name + i;
- val = " value=" + lst[i][0];
- text = lst[i][1];
- }
- if (lst[i][0] === value) { check = " checked"; } else { check = ""; }
- content = content + "<input type="radio" name="" + name + """ + val + check + readonly + " id="" + id + "" /><label for="" + id + "" class="mb-0">" + text + "</label>";
- }
- content = "<div class="col-auto">" + content + "</div>";
- o.html("<label class='col-md-2 col-form-label'>" + title + need + "</label><div class='col-md-10 row align-items-center'>" + content + "<div class='col-auto text-muted'>" + help + "</div></div>");
- };
- var submit = function (name, value) {
- var o = $("form ." + name);
- o.html("<label class='col-md-2 col-form-label'></label><div class="col-md-10"><input type='submit' name = 'submit' value = '" + value + "' class='btn btn-fst'/><input type='reset' name='reset' value='" + common.reset + "' class='btn btn-fst'/></div>");
- };
- var textarea = function (name, title, placeholder, value, help) {
- var o = $("form ." + name);
- var rows = "";
- var need = "";
- var required = "";
- var readonly = "";
- if (placeholder !== "") { placeholder = " placeholder="" + placeholder + """; }
- if (o.attr("rows") !== undefined) { rows = " rows="" + o.attr("rows") + """; }
- if (o.attr("need") !== undefined) {
- if (o.attr("need").indexOf("required") >= 0) { need = "<span class='red'>*</span>"; required = " required"; }
- if (o.attr("need").indexOf("readonly") >= 0) { readonly = " readonly"; }
- }
- o.html("<label class='col-md-2 col-form-label'>" + title + need + "</label><div class='col-md-10'><textarea name="" + name + "" class="form-control"" + placeholder + rows + readonly + required + ">" + value + "</textarea><div class='d-flex text-muted'>" + help + "</div></div>");
- };
- var bind = function (type, name, obj) {
- switch (type) {
- case "range":
- $("[name=" + name + "]").change(function () {
- $("[name=" + obj + "]").val($("[name = " + name + "]").val());
- price();
- });
- $("[name=" + obj + "]").change(function () {
- $("[name=" + name + "]").val($("[name = " + obj + "]").val());
- price();
- });
- break;
- }
- };
- $.extend({
- send: function (url, args, tar) {
- var body = $(document.body),
- form = $("<form method='post' target='" + tar + "'></form>"),
- input;
- form.attr({ "action": url });
- $.each(args, function (key, value) {
- input = $("<input type='hidden'>");
- input.attr({ "name": key });
- input.val(value);
- form.append(input);
- });
- form.appendTo(document.body);
- form.submit();
- document.body.removeChild(form[0]);
- }
- });
- var FormDataToJson = function (formData) {
- var objData = {};
- formData.forEach((value, key) => objData[key] = value);
- return JSON.stringify(objData);
- };
- var regex = {
- name:"^[\u4e00-\u9fa5_a-zA-Z0-9]+[ DISCUZ_CODE_0 ]quot;,
- domain: "^([a-z0-9][a-z0-9\-]*?\.(?:com|cn|net|org|gov|info|la|cc|co|ac.cn)(?:\.(?:cn|jp))?)[ DISCUZ_CODE_0 ]quot;,
- host: "^([a-z0-9][a-z0-9\-\.]*?\.(?:com|cn|net|org|gov|info|la|cc|co|jp|hk|kr))[ DISCUZ_CODE_0 ]quot;,
- ip: "^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})){3}[ DISCUZ_CODE_0 ]quot;,
- int: "^[1-9][0-9]*[ DISCUZ_CODE_0 ]quot;,
- age:"^[1-9][0-9]{0,2}[ DISCUZ_CODE_0 ]quot;,//1-999
- num: "^[1-9][0-9]{1,3}[ DISCUZ_CODE_0 ]quot;,//10-9999
- rate: "^0$|^1$|^0\.[0-9]*[1-9][ DISCUZ_CODE_0 ]quot;
- };
- var err = function (msg) {
- if (msg === "nologin") { top.href = "/login"; } else { alert(msg); }
- };
复制代码
|