Created
October 4, 2010 09:37
-
-
Save think49/609454 to your computer and use it in GitHub Desktop.
getFormElements.js : HTMLFormElement.elements をオブジェクトに変換して返す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>getFormElements.js</title> | |
<style type="text/css"><!-- | |
/* default */ | |
body { margin: 0; padding: 1em; } | |
h1,h2,h3,h4,h5,h6 { font-weight: bold; color: #202020; } | |
h1 { margin: 1em 0; padding: 0; font-size: 150%; } | |
h2 { margin: 2.5em 0 1em; padding: 0; font-size: 120%; } | |
h3 { margin: 1em 0: padding: 0; font-size: 100%; color: #373; } | |
dl { margin: 1em 0; padding: 0; } | |
dt { margin: 1.5em 1em 1em; padding: 0; color: #337; font-weight: bold; } | |
dd { margin: 1em; padding: 0; } | |
dt a { text-decoration: none; color: #337; } | |
strong { font-size: 100%; font-weight: bold; } | |
em { font-size: 100%; font-weight: normal; font-style: normal; text-decoration: underline; } | |
blockquote { margin: 1em 2em; padding: 0.5em 1em; border: dashed 1px orange; } | |
cite { font-family: italic; } | |
pre { margin: 1.5em 2em; padding: 0.5em; font-family: monospace; font-size: 100%; font-weight: normal; } | |
code { font-family: monospace; font-size: 90%; font-weight: normal; } | |
hr { margin: 2em 0; padding: 0; } | |
h1 code, h2 code, h3 code, h4 code, h5 code, h6 code, th code { font-weight: bold; } | |
table { border-collapse: collapse; table-layout: auto; margin: 1em 0; padding: 0; border: solid 1px #AAA; color: black; background-color: ghostwhite; } | |
thead, tbody { margin: 0; padding: 0; border-style: none; } | |
thead th { text-align: center; font-weight: bolder; color: #202020; background-color: lavender; } | |
tbody th { text-align: left; font-weight: bolder; color: #202020; } | |
th, td { margin: 0; padding: 0.5em 0.8em; border: solid 1px #AAA; } | |
.button { display: inline-block; margin: 2px; padding: 1px; border: outset 2px #E9E9E9; color: black; background-color: #E9E9E9; } | |
--></style> | |
<script type="text/javascript" src="./getFormElements.js"></script> | |
</head> | |
<body> | |
<h1>getFormElements.js (仮称)</h1> | |
<h2 id="Description">解説</h2> | |
<p>HTMLFormElements.elements から name, valueプロパティが定義されているノードを抽出して、オブジェクトに変換します。</p> | |
<ul> | |
<li>名称は後で変更する可能性大。</li> | |
<li>機能も大幅に変わる可能性があります。(最終目標はJSON化)</li> | |
</ul> | |
<h2 id="Sample">サンプル</h2> | |
<form onsubmit="console.log(getFormElements(this)); event.preventDefault ? event.preventDefault() : event.returnValue = false;"> | |
<p>A1 <input type="text" name="a1" value=""></p> | |
<p>A2 <input type="text" name="a2" value=""></p> | |
<p>A3 <input type="text" name="a3" value=""></p> | |
<p>A3 <input type="text" name="a3" value=""></p> | |
<p>A4 <input type="text" name="a4" value=""></p> | |
<p><input type="submit" value="get HTMLFormElement.elements"></p> | |
</form> | |
<h2 id="ReferenceURL">参考URL</h2> | |
<ul> | |
<li><a href="http://d.hatena.ne.jp/babu_babu_baboo/20101001#c1285934655">2010-10-01 - babu_babu_babooのごみ箱</a></li> | |
<li><a href="http://www.tagindex.com/cgi-lib/q4bbs/patio.cgi?mode=view&no=2701">掲示板/JavaScript質問板/innerHTMLから値の取得 - TAG index Webサイト</a></li> | |
</ul> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getFormElements (form) { | |
var result, elements, element, name, temp, value, i, l; | |
elements = form.elements; | |
if (typeof form !== 'object' || typeof elements !== 'object' || typeof form.submit !== 'function') throw new TypeError(form + 'is not a HTMLFormElement'); | |
result = {}; | |
for (i = 0, l = elements.length; i < l; i++) { | |
element = elements[i]; | |
if ('name' in element && element.name.length && 'value' in element) { | |
name = element.name; | |
value = element.value; | |
if (elements[name].length) { | |
if (name in result) { | |
result[name].push(value); | |
} else { | |
result[name] = [value]; | |
} | |
} else { | |
result[name] = value; | |
} | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gtltさんからアドバイスをいただきました。
2010-10-01 - babu_babu_babooのごみ箱
http://d.hatena.ne.jp/babu_babu_baboo/20101001#c1285934655
TAG index で回答しました。
掲示板/JavaScript質問板/innerHTMLから値の取得 - TAG index Webサイト
http://www.tagindex.com/cgi-lib/q4bbs/patio.cgi?mode=view&no=2701