Jojoxx.net logotyp

 

 

Användare ikonAnvändare
Besökare just nu: 59
Snabblänkar ikonSnabblänkar
JavaScript-arkivet
Perl
FAQ

Sök på Jojoxx.net


Spara formulärsfält

Exempel

Text
Textarea
Select
Select (multiple)
Checkbox
Radiobox
Ladda om

Kod


<form>
<table>
<tr><td>Text</td><td><input type="text" name="field-1"></td></tr>
<tr><td>Textarea</td><td><textarea name="field-2"></textarea></td></tr>
<tr><td>Select</td><td><select name="field-3" size="2" multiple><option value="ab">ab</option><option value="cd">cd</option></select></td></tr>
<tr><td>Select (multiple)</td><td><select name="field-4"><option value="ab">ab</option><option value="cd">cd</option></select></td></tr>
<tr><td>Checkbox</td><td><input type="checkbox" name="field-5"></td></tr>
<tr><td>Radiobox</td><td><input type="radio" name="field-6" value="ab"> <input type="radio" name="field-6" value="cd"></td></tr>
</table>
</form>
<script type="text/javascript">
function session(){
	this._save=function(){
		this._raw="";
		for(var n in this){
			if(n.substring(0,1)!="_"){
				if(typeof(this[n])=="string"){
					this._raw+=escape(n)+":"+escape(this[n])+"/";
				} else {
					this._raw+=escape(n)+":[";
					for(var i=0;i<this[n].length;i++){
						this._raw+=escape(this[n][i]);
						if(i<this[n].length-1){ this._raw+=","; }
					}
					this._raw+="]/";
				}
			}
		}
		for(var n=0;n<20&&n<this._raw.length/4000;n++){
			document.cookie="jsess"+n+"="+this._raw.substring(n*4000,(n+1)*4000);
		}
	}
	this._variables=function(){
		var _vars=new Array();
		for(var n in this){
			if(n.substring(0,1)!="_"){
				_vars[_vars.length]=n;
			}
		}
		return _vars;
	}
	this._clear=function(){
		for(var n in this){ if(n.substring(0,1)!="_"){ delete(this[n]); } }
		delete(this._raw);
		delete(this._pairs);
	}
	var cookies=document.cookie.split("; ").sort(); this._raw="";
	for(var n=0;n<cookies.length;n++){
		if(unescape(cookies[n].split("=")[0]).substring(0,5)=="jsess"){ this._raw+=cookies[n].split("=")[1]; }
	}
	this._pairs=this._raw.split("/");
	for(var n=0;n<this._pairs.length-1;n++){
		if(this._pairs[n].split(":")[1].substring(0,1)=="["){
			var vals=this._pairs[n].split(":")[1]; vals=vals.substring(1,vals.length-1); vals=vals.split(",");
			this[unescape(this._pairs[n].split(":")[0])]=new Array();
			for(var nn=0; nn<vals.length; nn++){
				this[unescape(this._pairs[n].split(":")[0])][this[unescape(this._pairs[n].split(":")[0])].length]=unescape(vals[nn]);
			}			
		} else {
			this[unescape(this._pairs[n].split(":")[0])]=unescape(this._pairs[n].split(":")[1]);
		}
	}	
}
function populateForm(obj,listobj){
	for(var n=0;n<obj.length;n++){
		if(obj[n].type == "select-one" && listobj[obj[n].name]){
			for(var nn=0;nn<obj[n].length;nn++){
				if(obj[n][nn].value==listobj[obj[n].name]){ obj[n].selectedIndex=nn; }
			}
		} else if(obj[n].type == "select-multiple" && listobj[obj[n].name]){
			if(typeof(listobj[obj[n].name])=="string"){
				for(var nn=0;nn<obj[n].length;nn++){
					if(obj[n][nn].value==listobj[obj[n].name]){ obj[n][nn].selected=true; } else { obj[n][nn].selected=false; }
				}
			}else{
				for(var nn=0;nn<obj[n].length;nn++){
					obj[n][nn].selected=false;
					for(var nnn=0;nnn<listobj[obj[n].name].length;nnn++){
						if(obj[n][nn].value==listobj[obj[n].name][nnn]){ obj[n][nn].selected=true; }
					}
				}
			}
		} else if(obj[n].type == "text" && listobj[obj[n].name]){
			obj[n].value=listobj[obj[n].name];
		} else if(obj[n].type == "radio" && listobj[obj[n].name]){
			obj[n].checked=(obj[n].value==listobj[obj[n].name]);
		} else if(obj[n].type == "checkbox" && listobj[obj[n].name]){
			obj[n].checked=(listobj[obj[n].name]);
		} else if(obj[n].type == "hidden" && listobj[obj[n].name]){
			obj[n].value=listobj[obj[n].name];
		} else if(obj[n].type == "textarea" && listobj[obj[n].name]){
			obj[n].value=listobj[obj[n].name];
		}
	}
}
function saveform(obj){
	mysession._clear();
	for(var i=0;i<obj.elements.length;i++){
		if(obj.elements[i].name){
			if(obj.elements[i].type=="select-multiple"){
				mysession[obj.elements[i].name]=new Array();
				for(var ii=0;ii<obj.elements[i].length;ii++){
					if(obj.elements[i][ii].selected){ mysession[obj.elements[i].name][mysession[obj.elements[i].name].length]=obj.elements[i][ii].value; }
				}
			} else if(obj.elements[i].type == "radio" && obj.elements[i].checked){
				mysession[obj.elements[i].name]=obj.elements[i].value;
			} else if(obj.elements[i].type == "checkbox" && obj.elements[i].checked){
				mysession[obj.elements[i].name]=1;
			} else if(obj.elements[i].type == "select-one" && obj.elements[i].selectedIndex){
				mysession[obj.elements[i].name]=obj.elements[i][obj.elements[i].selectedIndex].value;
			} else if(obj.elements[i].type == "text" || obj.elements[i].type == "textarea"){
				mysession[obj.elements[i].name]=obj.elements[i].value;
			}
		}
	}
	mysession._save();
}
window.onload = function(){
	mysession=new session();
	populateForm(document.forms[0],mysession);
}
window.onunload = function(){
	saveform(document.forms[0]);
}
</script>
<a href="#" onclick="location.reload(1);">Ladda om</a><br>

Kommentar

Scriptet använder session-variabler för att spara innehållet i ett formulär om sidan laddas om.

,