2 回答
TA贡献1842条经验 获得超21个赞
相当复杂的任务,主要在 IE 11 中测试?在开发过程中 FF 遇到了一些问题,但现在它似乎也可以在 FF 和 Chrome 中工作,但在那里没有进行太多测试。
var myForm = document.forms[0],
formData;
myForm.countries.selectedIndex = -1;
formData = getFormData(myForm[0]);
function saveForm(i) {
var values = getFormData(i);
// show JSON read
document.getElementById("saving").innerText = JSON.stringify(values, null, 1)
.replace(/\n "/g, '\n')
.replace(/\n*[\{\}]\n*/g, '')
.replace(/,\n/g, '\n')
.replace(/([^"]+)":/g, '$1:');
if (prompt('Clear data now ?', 'Yes will load them by file button') != null) setFormData(myForm, formData);
return downloadObjectAsJson(values, "test");
}
function loadBack(t) {
var f = t.files[0];
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
var json = JSON.parse(e.target.result);
setFormData(myForm, json);
return;
}
})(f);
// Read in the image file as a data URL.
reader.readAsText(f);
}
function setFormData(frm, values) {
for (var v in values) {
var el = frm[v],
val = values[v];
if (el.constructor == HTMLCollection) {
// case "radio":
// values[el.name].checked = val;
// break;
for (var p = 0; p < el.length; p++) {
el[p].checked = (el[p].value === val);
}
} else switch (el.type) {
case "checkbox":
el.checked = val;
break;
case 'select-one':
if (val < 0) el.selectedIndex = val;
else {
for (var o = 0; o < el.options.length; o++) {
if (el.options[o].value == val) el.selectedIndex = o;
}
}
break;
case 'select-multiple':
for (var o = 0; o < el.options.length; o++) {
el.options[o].selected = (val.indexOf(el.options[o].value) > -1);
}
break;
default:
el.value = val;
}
}
}
function getFormData(i) {
var frm = i.form;
var values = {};
for (var a = 0; a < frm.length; a++) {
var el = frm[a];
switch (el.type) {
case "checkbox":
values[el.name] = el.checked;
break;
case "radio":
if (el.checked) values[el.name] = el.value;
else if (values[el.name] === undefined) values[el.name] = false;
break;
case 'select-one':
values[el.name] = el.selectedIndex < 0 ? -1 : el.options[el.selectedIndex].value;
break;
case 'select-multiple':
values[el.name] = [];
for (var i = 0; i < el.options.length; i++) {
if (el.options[i].selected) values[el.name].push(el.options[i].value);
}
break;
case 'fieldset':
break;
case 'button':
break;
case 'submit':
break;
case 'reset':
break;
case 'file':
break;
case undefined:
break;
default:
values[el.name] = el.value;
}
}
return values;
}
function downloadObjectAsJson(exportObj, exportName) {
if (navigator.msSaveBlob) { // IE10+
var blob = new Blob([JSON.stringify(exportObj, null, 1)], {
type: 'application/json'
});
return navigator.msSaveBlob(blob, exportName + ".json");
}
var dataStr = "data:application/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj, null, 1));
var downloadAnchorNode = document.getElementsByName('_saveAnchor')[0];
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", exportName + ".json");
downloadAnchorNode.click();
downloadAnchorNode.remove();
}
<!-- form elements from https://www.fincher.org/tips/web/SimpleForm.shtml -->
<form>
First Name: <input type="text" name="FirstName"/><br />
<textarea name="comments"></textarea><br />
Password:<input type="password" name="mypassword" value="start"/> „start“ as test value you can get from server<br />
<input type="radio" name="title" value="mr" />Mr.<br />
<input type="radio" name="title" value="ms" />Ms.<br />
<input type="radio" name="title" value="decline"/>decline<br />
<fieldset style="margin: 1em; text-align: center;">
<legend>Select a Hero</legend>
<input type="radio" name="hero" value="Agamemnon" /> Agamemnon
<input type="radio" name="hero" value="Achilles" /> Achilles
<input type="radio" name="hero" value="Hector" /> Hector
<div style="height: 1em;" />
</fieldset>
<input type="checkbox" name="mail" />Mail me more info<br />
<input type="checkbox" name="e-mail" />E-Mail me more info<br />
<select name="countries">
<option>Argentina</option>
<option>Albania</option>
<option>Afganistain</option>
<option>Aruba</option>
</select>
<select name="menuitems">
<optgroup id="opt1" label="food">
<option>Hamburgers</option>
<option>Hotdogs</option>
</optgroup>
<optgroup id="opt2" label="drinks">
<option>Coke</option>
<option>Pepsi</option>
<option>Water</option>
</optgroup>
</select>
<select name="computerbrands" multiple="multiple">
<option value="DELL">DELL</option>
<option value="IBM">IBM</option>
<option value="HP">HP</option>
<option value="Compaq">Compaq</option>
<option value="Sony">Sony</option>
</select>
<select name="countries2" size="4">
<option value="0">Argentina</option>
<option value="1">Albania</option>
<option value="2">Afganistain</option>
<option value="3">Aruba</option>
<option value="4">Arabia</option>
</select>
<input type="hidden" name="computerType" value="hidden DELL" />
<input type="submit" name="submitbutton" value="Submit" />
<a name="_saveAnchor"><input type="button" value="Save content as JSON" onclick="saveForm(this)"></a>
<input type="file" name="formFile" onchange="loadBack(this)"/>
</form>
<div id="saving"></div>
制作了 2 个新书签,唯一的问题是找到合适的形式......
这 1 会将最长的形式保存为 JSON
javascript:f=document.forms,longest=0;frm=0;for(i=0;i<f.length;i++)if(f[i].length>longest){longest=f[i].length;frm=f[i]}values={};for(a=0;a<longest;a++){el=frm[a];switch(el.type){case"checkbox":values[el.name]=el.checked;break;case"radio":if(el.checked)values[el.name]=el.value;else if(values[el.name]===undefined)values[el.name]=false;break;case'select-one':values[el.name]=el.selectedIndex<0?-1:el.options[el.selectedIndex].value;break;case'select-multiple':values[el.name]=[];for(i=0;i<el.options.length;i++){if(el.options[i].selected)values[el.name].push(el.options[i].value)}break;case'fieldset':break;case'button':break;case'submit':break;case'reset':break;case'file':break;case undefined:break;default:values[el.name] = el.value}}dataStr="data:application/json;charset=utf-8,"+encodeURIComponent(JSON.stringify(values));dwn=document.createElement('A');dwn.setAttribute("href",dataStr);dwn.setAttribute("download",location.href.split('/').pop()+".json");dwn.click();dwn.remove();undefined
或者格式化内容
f = document.forms, longest = 0; frm = 0;
for (i = 0; i < f.length; i++)
if (f[i].length > longest) { longest = f[i].length; frm = f[i] }
values = {};
for (a = 0; a < longest; a++) {
el = frm[a];
switch (el.type) {
case "checkbox": values[el.name] = el.checked;
break;
case "radio":
if (el.checked)
values[el.name] = el.value;
else if (values[el.name] === undefined)
values[el.name] = false;
break;
case 'select-one': values[el.name] = el.selectedIndex < 0 ? -1 : el.options[el.selectedIndex].value; break;
case 'select-multiple': values[el.name] = [];
for (i = 0; i < el.options.length; i++) {
if (el.options[i].selected)
values[el.name].push(el.options[i].value)
}
break;
case 'fieldset': break;
case 'button': break;
case 'submit': break;
case 'reset': break;
case 'file': break;
case undefined: break;
default: values[el.name] = el.value
}
} dataStr = "data:application/json;charset=utf-8," + encodeURIComponent(JSON.stringify(values));
dwn = document.createElement('A');
dwn.setAttribute("href", dataStr);
dwn.setAttribute("download", location.href.split('/').pop() + ".json");
dwn.click();
dwn.remove(); undefined
这将从JSON 读取表单
javascript:function setFormData(values){f=document.forms,longest=0;frm=0;for(i=0;i<f.length;i++)if(f[i].length>longest){longest=f[i].length;frm=f[i]}for(v in values){el=frm[v];val=values[v];if(el.constructor==HTMLCollection){for(p=0;p<el.length;p++){el[p].checked=(el[p].value===val)}}else switch(el.type){case"checkbox":el.checked=val;break;case'select-one':if(val<0)el.selectedIndex=val;else{for(o=0;o<el.options.length;o++){if(el.options[o].value==val)el.selectedIndex=o}}break;case'select-multiple':for(o=0;o<el.options.length;o++){el.options[o].selected=(val.indexOf(el.options[o].value)>-1)}break;default:el.value=val}}}f=document.createElement("input");f.type="file";f.onchange=(function(){reader=new FileReader();reader.onload=(function(theFile){return function(e){json = JSON.parse(e.target.result);setFormData(json);return;}})(event.srcElement.files[0]);reader.readAsText(event.srcElement.files[0]);});document.body.prepend(f);f.focus();
和这个:
function setFormData(values) {
f = document.forms, longest = 0; frm = 0;
for (i = 0; i < f.length; i++)
if (f[i].length > longest) {
longest = f[i].length; frm = f[i]
}
for (v in values) {
el = frm[v]; val = values[v];
if (el.constructor == HTMLCollection) {
for (p = 0; p < el.length; p++) {
el[p].checked = (el[p].value === val)
}
} else switch (el.type) {
case "checkbox": el.checked = val;
break;
case 'select-one':
if (val < 0) el.selectedIndex = val;
else {
for (o = 0; o < el.options.length; o++) {
if (el.options[o].value == val) el.selectedIndex = o
}
}
break;
case 'select-multiple':
for (o = 0; o < el.options.length; o++) {
el.options[o].selected = (val.indexOf(el.options[o].value) > -1)
}
break;
default: el.value = val
}
}
}
f = document.createElement("input");
f.type = "file";
f.onchange = (function () {
reader = new FileReader();
reader.onload = (function (theFile) {
return function (e) {
json = JSON.parse(e.target.result);
setFormData(json);
return;
}
})(event.srcElement.files[0]);
reader.readAsText(event.srcElement.files[0]);
});
document.body.prepend(f);
f.focus();
制作了另外 2 个书签,现在使用 aria-label 和 className (hasOutput) 等。
javascript:f=document.forms,longest=0;frm=0;for(i=0;i<f.length;i++)if(f[i].length>longest){longest=f[i].length;frm=f[i]}values={};for(a=0;a<longest;a++){el=frm[a];switch(el.type){case "checkbox":values[el.name]=el.checked;break;case "radio":if(el.checked)values[el.name]=el.value;else if(values[el.name]===undefined){values[el.name]=false;continue}break;case "select-one":values[el.name]=el.selectedIndex<0?-1:el.options[el.selectedIndex].value;break;case "select-multiple":values[el.name]=[];for(i=0;i<el.options.length;i++){if(el.options[i].selected)values[el.name].push(el.options[i].value)}break;case "fieldset":case "button":case "submit":case "reset":case "file":case "hidden":case undefined:continue;default:values[el.name]=el.value}var old=values[el.name];delete values[el.name];if(!old)continue;var names=[],passed={};do{var el2=el;do{if(el2.className=="hasOutput"&&!passed[el2.innerText]){names.push(el2.innerText);passed[el2.innerText]=1}el2=el2.firstElementChild}while(el2)if(el.ariaLabel&&!passed[el.ariaLabel]){names.push(el.ariaLabel.trim());passed[el.ariaLabel]=1}}while(el=el.parentElement)if(names[0]=="Měna"&&old=="aed")continue;names.reverse();el=values;for(b=0;b<names.length-1;b++){if(!el[names[b]])el[names[b]]={};el=el[names[b]]}el[names[names.length-1]]=old}dataStr="data:application/json;charset=utf-8,"+encodeURIComponent(JSON.stringify(values));dwn=document.createElement("A");dwn.setAttribute("href",dataStr);dwn.setAttribute("download",location.href.split("/").pop()+".json");dwn.click();dwn.remove();undefined
f = document.forms, longest = 0; frm = 0;
for (i = 0; i < f.length; i++)
if (f[i].length > longest) { longest = f[i].length; frm = f[i] }
values = {};
for (a = 0; a < longest; a++) {
el = frm[a];
switch (el.type) {
case "checkbox": values[el.name] = el.checked;
break;
case "radio":
if (el.checked)
values[el.name] = el.value;
else if (values[el.name] === undefined)
{
values[el.name] = false;
continue;
}
break;
case "select-one": values[el.name] = el.selectedIndex < 0 ? -1 : el.options[el.selectedIndex].value; break;
case "select-multiple": values[el.name] = [];
for (i = 0; i < el.options.length; i++) {
if (el.options[i].selected)
values[el.name].push(el.options[i].value)
}
break;
case "fieldset":
case "button":
case "submit":
case "reset":
case "file":
case "hidden":
case undefined:
continue;
break;
default: values[el.name] = el.value
}
var old = values[el.name];
delete values[el.name];
if (!old) continue;
var names = [], passed = {};
do {
var el2 = el;
do {
if (el2.className == "hasOutput" && !passed[el2.innerText])
{
names.push(el2.innerText);
passed[el2.innerText] = 1;
}
el2 = el2.firstElementChild;
} while (el2)
if (el.ariaLabel && !passed[el.ariaLabel])
{
names.push(el.ariaLabel.trim());
passed[el.ariaLabel] = 1;
}
} while (el = el.parentElement)
if (names[0] == "Měna" && old == "aed") continue;
names.reverse();
el = values;
for(b = 0; b < names.length - 1; b++)
{
if (!el[names[b]])
el[names[b]] = {}
el = el[names[b]];
}
el[names[names.length-1]] = old;
}
dataStr = "data:application/json;charset=utf-8," + encodeURIComponent(JSON.stringify(values));
dwn = document.createElement("A");
dwn.setAttribute("href", dataStr);
dwn.setAttribute("download", location.href.split("/").pop() + ".json");
dwn.click();
dwn.remove(); undefined
然后回来
javascript:function setFormData(valuesBack){f=document.forms,longest=0;frm=0;for(i=0;i<f.length;i++)if(f[i].length>longest){longest=f[i].length;frm=f[i]}values={};for(a=0;a<longest;a++){el=frm[a];switch(el.type){case "checkbox":values[el.name]=el;break;case "radio":if(el.checked)values[el.name]=el;else if(values[el.name]===undefined){values[el.name]=el;continue}break;case "select-one":values[el.name]=el;break;case "select-multiple":values[el.name]=el;break;case "fieldset":case "button":case "submit":case "reset":case "file":case "hidden":case undefined:continue;break;default:values[el.name]=el}var names=[],passed={};var oldEl=values[el.name];do{var el2=el;do{if(el2.className=="hasOutput"&&!passed[el2.innerText]){names.push(el2.innerText);passed[el2.innerText]=1}el2=el2.firstElementChild}while(el2)if(el.ariaLabel&&!passed[el.ariaLabel]){names.push(el.ariaLabel.trim());passed[el.ariaLabel]=1}}while(el=el.parentElement)names.reverse();el=valuesBack;for(b=0;b<names.length&⪙b++){el=el[names[b]]}if(el===undefined)continue;val=el;el=oldEl;if(el.constructor==HTMLCollection){for(p=0;p<el.length;p++){el[p].checked=(el[p].value===val)}}else switch(el.type){case "checkbox":el.checked=val;break;case "select-one":if(val<0)el.selectedIndex=val;else{for(o=0;o<el.options.length;o++){if(el.options[o].value==val)el.selectedIndex=o}}break;case "select-multiple":for(o=0;o<el.options.length;o++){el.options[o].selected=(val.indexOf(el.options[o].value)>-1)}break;default:el.value=val}}}f=document.createElement("input");f.type="file";f.onchange=(function(){reader=new FileReader();reader.onload=(function(theFile){return function(e){json=JSON.parse(e.target.result);setFormData(json);return}})(event.srcElement.files[0]);reader.readAsText(event.srcElement.files[0])});document.body.prepend(f);f.focus();
function setFormData(valuesBack) {
f = document.forms, longest = 0; frm = 0;
for (i = 0; i < f.length; i++)
if (f[i].length > longest) { longest = f[i].length; frm = f[i] }
values = {};
for (a = 0; a < longest; a++) {
el = frm[a];
switch (el.type) {
case "checkbox": values[el.name] = el;
break;
case "radio":
if (el.checked)
values[el.name] = el;
else if (values[el.name] === undefined)
{
values[el.name] = el;
continue;
}
break;
case "select-one": values[el.name] = el; break;
case "select-multiple": values[el.name] = el;
break;
case "fieldset":
case "button":
case "submit":
case "reset":
case "file":
case "hidden":
case undefined:
continue;
break;
default: values[el.name] = el
}
var names = [], passed = {};
var oldEl = values[el.name];
do {
var el2 = el;
do {
if (el2.className == "hasOutput" && !passed[el2.innerText])
{
names.push(el2.innerText);
passed[el2.innerText] = 1;
}
el2 = el2.firstElementChild;
} while (el2)
if (el.ariaLabel && !passed[el.ariaLabel])
{
names.push(el.ariaLabel.trim());
passed[el.ariaLabel] = 1;
}
} while (el = el.parentElement)
names.reverse();
el = valuesBack;
for(b = 0; b < names.length && el; b++)
{
el = el[names[b]];
}
if (el === undefined) continue;
val = el; el = oldEl;
if(val === true) val = false;
else val = val.split('').reverse().join('');
if (el.constructor == HTMLCollection) {
for (p = 0; p < el.length; p++) {
el[p].checked = (el[p].value === val)
}
} else switch (el.type) {
case "checkbox": el.checked = val;
break;
case "select-one":
if (val < 0) el.selectedIndex = val;
else {
for (o = 0; o < el.options.length; o++) {
if (el.options[o].value == val) el.selectedIndex = o
}
}
break;
case "select-multiple":
for (o = 0; o < el.options.length; o++) {
el.options[o].selected = (val.indexOf(el.options[o].value) > -1)
}
break;
default: el.value = val
}
}
}
f = document.createElement("input");
f.type = "file";
f.onchange = (function () {
reader = new FileReader();
reader.onload = (function (theFile) {
return function (e) {
json = JSON.parse(e.target.result);
setFormData(json);
return;
}
})(event.srcElement.files[0]);
reader.readAsText(event.srcElement.files[0]);
});
document.body.prepend(f);
f.focus();
TA贡献1868条经验 获得超4个赞
有多种解决方案可以满足您的要求,问题是数据持久性。我鼓励采用一种解决方案,将表单提交和正在进行的表单提交保存到数据库或云中。如果您有兴趣,我们可以讨论这些。
一般来说,Web 浏览器并不是为了简单地在客户端上创建、保存和加载数据而设计的。服务器将参与该过程。但是,可以选择在任何现代浏览器上使用本地存储。它比使用 cookie 更强大。
每个表单提交还需要一个 ID(如日期/时间戳),以及一个用于选择要加载的表单或创建新表单提交的对话框。
- 2 回答
- 0 关注
- 112 浏览
添加回答
举报