JS Implement Dynamic Form method

Source: Internet
Author: User

Using JavaScript to implement a relatively simple method, for all the form elements used only for display, to get their values, in the form of "key=value,key=value" stored in the database, only one text field in the database can be, when modified, from the database to get these values, The corresponding element is assigned a value according to the key.

Primarily using Data-skey custom attributes as key, form elements that support Select, textarea, and input type text, radio, checkbox

The code is as follows Copy Code
function Dynamicform (container) {
This.container = typeof (Container) = = ' String '? document.getElementById (container): container;
}
Dynamicform.prototype = {
_keyvaluesplitter: ' = ',
_itemsplitter: ', ',
_attrkey: ' Data-skey ',
_chktypes: [' checkbox ', ' Radio '],
_isintype:function (A, EL) {
var etype = El.type.toLowerCase ();
var i = a.length;
while (i--) {
if (a[i] = = etype) {
return true;
}
}
return false;
},
_addelement:function (array, tag, types) {
var el, nodes = this.container.getElementsByTagName (tag);
for (var i = 0, L = nodes.length i < l; i++) {
El = Nodes[i];
if (!types | | (Types && This._isintype (types, EL)) {
Array.push (EL);
}
}
},
_getelements:function () {
var ret = [];
This._addelement (ret, ' input ', [' Text '].concat (this._chktypes));
This._addelement (ret, ' textarea ');
This._addelement (ret, ' select ');
return ret;
},
/* Get the value of all elements * *
Getvalues:function () {
var Elarray = this._getelements (), ret = [], El, key, Ischk;
for (var i = 0, L = elarray.length i < l; i++) {
El = Elarray[i];
Key = El.getattribute (This._attrkey);
if (key) {
Ischk = This._isintype (This._chktypes, EL);
if ((!ischk && el.value) | | (Ischk && el.checked))
Ret.push (key + This._keyvaluesplitter + encodeuricomponent (el.value));
}
}
Return Ret.join (This._itemsplitter);
},
_setvalues:function (Val, Elarray, callback) {
if (!) ( val | | val.length)) return;
var Elen = elarray.length, Valarray = Val.split (this._itemsplitter), keyvalue;
for (var i = 0, L = valarray.length i < l; i++) {
KeyValue = Valarray[i].split (This._keyvaluesplitter);
Callback.call (This, Elarray, Elen, keyvalue);
}
},
/* Assign values to all elements (can be used to modify the page) * *
Setvalues:function (val) {
if (!) ( val | | val.length)) return;
This._setvalues (Val, this._getelements (), this._setelementvalue);
},
_setelementvalue:function (Elarray, Elen, keyvalue) {
var el, key = Keyvalue[0], val = decodeuricomponent (keyvalue[1]), Hasset;
for (var i = 0; i < Elen; i++) {
El = Elarray[i];
if (El.getattribute (this._attrkey) = = key) {
if (This._isintype (This._chktypes, el)) {
if (val = = El.value)
Hasset = el.checked = true;
}
else if (Val) {
El.value = val;
Hasset = true;
}
if (Hasset) {
Elarray.splice (i, 1);
Break
}
}
}
},
/* Used to display to the page (read only) * *
Showto:function (val, tag) {
if (!) ( val | | val.length)) return;
var ret = [];
This._addelement (ret, tag);
This._setvalues (Val, ret, this._showtoelement);
},
_showtoelement:function (Elarray, Elen, keyvalue) {
var el, key = Keyvalue[0], val = decodeuricomponent (keyvalue[1]), Hasset;
for (var i = 0; i < Elen; i++) {
El = Elarray[i];
if (El.getattribute (this._attrkey) = = key) {
el.innerhtml = val;
Elarray.splice (i, 1);
Break
}
}
}
};

Test code

The code is as follows Copy Code
<! DOCTYPE html>
<title></title>
<script src= "Dynamicform-compiled.js" ></script>
<script>
function SaveData () {
var f = new Dynamicform (' Form-wrapper ');
document.getElementById (' Hf1 '). Value = F.getvalues ();
}
function Restoredata () {
var f = new Dynamicform (' Form-wrapper ');
F.setvalues (document.getElementById (' hf1 '). Value);
}
function ShowData () {
var f = new Dynamicform (' View-wrapper ');
F.showto (document.getElementById (' hf1 '). Value, ' span ');
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div id= "Form-wrapper" >
<asp:literal runat= "Server" id= "View1"/>
</div>
<asp:hiddenfield id= "HF1" runat= "Server"/>
<asp:button id= "B1" runat= "Server" text= "Submit" onclick= "B1_click" onclientclick= "SaveData ()"/>
<input type= "button" value= "restore Form" onclick= "Restoredata ()"/>
<div id= "View-wrapper" >
<asp:literal runat= "Server" id= "View2"/>
</div>
</form>
</body>

asp.net code

The code is as follows Copy Code

public partial class Test_default:page
{
protected override void OnLoad (EventArgs e)
{
View1. Text = File.readalltext (Server.MapPath ("template-form.html"));
Base. OnLoad (e);
}

protected void B1_click (object sender, EventArgs e)
{
View2. Text = File.readalltext (Server.MapPath ("template-view.html"));
Clientscript.registerstartupscript (this. GetType (), Guid.NewGuid (). ToString (), "ShowData ();", true);
}
}

Template data for modification

  code is as follows copy code

Name: <input type=" text "data-skey=" name "/>
<br/>
Sex:
& Lt;input type= "Radio" name= "Rdo_group1" value= "male" data-skey= "gender"/> Male
<input type= "Radio" name= "Rdo_" Group1 "value=" female "data-skey=" gender "/> Female
<br/>
who can see:
<select data-skey=" seeing "
 & nbsp;  <option value= "" > Please select </option>
    <option value= "Anyone" > anyone </ Option>
    <option value= "only Friends" > Only friends </option>
    <option Value= "Confidential" > Confidential </option>
</select>
<br/>
Self-Introduction:
<textarea data-skey= "Intro" ></textarea>

Template file

The code is as follows Copy Code

Name: <span data-skey= "Name" ></span>
<br/>
Gender:
<span data-skey= "Gender" ></span>
<br/>
Who can see:
<span data-skey= "" "></span>
<br/>
Self Introduction:
<span data-skey= "Intro" ></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.