define²¿·Ö¶¨ÒåÒ»¸ö¼òµ¥µÄÄ£°åÀ࣬ʹÓÃ{}×÷ΪתÒå±ê¼Ç£¬ÖмäµÄÊý×Ö±íÊ¾Ìæ»»Ä¿±ê£¬formatʵ²ÎÓÃÀ´Ì滻ģ°åÄÚ±ê¼Ç ºáÏß´¦Ì
Array.prototype.slice.call(arguments, 0)
/\{\s*(\d+)\s*\}/g
±àдһ¸öº¯ÊýʵÏÖformµÄÐòÁл¯(¼´½«Ò»¸ö±íµ¥ÖеļüÖµÐòÁл¯Îª¿ÉÌá½»µÄ×Ö·û´®)
<form>
<select>
<option value="aaa">aaa</option>
<option value="bbb" selected>bbb</option>
</select>
<select multiple>
<option value="qiu" selected>qiu</option>
<option value="de">de</option>
<option value="qing" selected>qing</option>
</select>
<input value="qiudeqing">
<input type="password" value="11111">
<input type="hidden" value="3333">
<textarea>description</textarea>
<input type="checkbox" checked value="football">Football
<input type="checkbox" value="basketball">Basketball
<input type="radio" checked value="Female">Female
<input type="radio" value="Male">Male
</form>
<script>
/**
* ½«Ò»¸ö±íµ¥ÔªËØÐòÁл¯Îª¿ÉÌá½»µÄ×Ö·û´®
*
* @param {FormElement} form ÐèÒªÐòÁл¯µÄ±íµ¥ÔªËØ
* @return {string} ±íµ¥ÐòÁл¯ºóµÄ×Ö·û´®
*/
function serializeForm(form) {
if (!form || form.nodeName.toUpperCase() !== 'FORM') {
return;
}
var result = [];
var i, len;
var field, fieldName, fieldType;
for (i = 0, len = form.length; i < len; ++i) {
field = form.elements[i];
fieldName = field.name;
fieldType = field.type;
if (field.disabled || !fieldName) {
continue;
} // enf if
switch (fieldType) {
case 'text':
case 'password':
case 'hidden':
case 'textarea':
result.push(encodeURIComponent(fieldName) + '=' +
encodeURIComponent(field.value));
break;
case 'radio':
case 'checkbox':
if (field.checked) {
result.push(encodeURIComponent(fieldName) + '=' +
encodeURIComponent(field.value));
}
break;
case 'select-one':
case 'select-multiple':
for (var j = 0, jLen = field.options.length; j < jLen; ++j) {
if (field.options[j].selected) {
result.push(encodeURIComponent(fieldName) + '=' +
encodeURIComponent(field.options[j].value || field.options[j].text));
}
} // end for
break;
case 'file':
case 'submit':
break; // ÊÇ·ñ´¦Àí£¿
default:
break;
} // end switch
} // end for
return result.join('&');
}
var form = document.getElementById('target');
console.log(serializeForm(form));
</script>
ʹÓÃÔÉújavascript¸øÏÂÃæÁбíÖеÄli½Úµã°ó¶¨µã»÷ʼþ,µã»÷ʱ´´½¨Ò»¸öObject¶ÔÏó,¼æÈÝIEºÍ±ê×¼ä¯ÀÀÆ÷
<ul>
<li><a href="http://11111">111</a></li>
<li><a href="http://2222">222</a></li>
<li><a href="http://333">333</a></li>
<li><a href="http://444">444</a></li>
</ul>
Object:
{
"index": 1,
"name": "111",
"link": "http://1111"
}
¡¡