回答編集履歴
2
調整
answer
CHANGED
@@ -41,6 +41,8 @@
|
|
41
41
|
# 調整版
|
42
42
|
|
43
43
|
```
|
44
|
+
$(function () {
|
45
|
+
$("button").on('click', function (e) {
|
44
46
|
var f=$(this).prop('form').elements;
|
45
47
|
var n=[];
|
46
48
|
for(i=0;i<f.length;i++){
|
1
調整
answer
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
```HTML
|
4
4
|
<form>
|
5
5
|
<input type="text" name="t1" value="123">
|
6
|
+
<input type="text" name="t2" value="456" disabled>
|
7
|
+
<input type="hidden" name="h1" value="789">
|
6
8
|
<select name="s1">
|
7
9
|
<option value="1">1</option>
|
8
10
|
<option value="2" selected>2</option>
|
@@ -15,8 +17,10 @@
|
|
15
17
|
<input type="radio" name="r1" value="2">
|
16
18
|
<input type="radio" name="r1" value="3">
|
17
19
|
<button type="button">submit</button>
|
20
|
+
<textarea name="ta1">ta</textarea>
|
18
21
|
</form>
|
19
22
|
|
23
|
+
|
20
24
|
```
|
21
25
|
のようなフォームに対して、以下のように値を得ます
|
22
26
|
|
@@ -34,18 +38,25 @@
|
|
34
38
|
```
|
35
39
|
ただしfor~ofはIEが対応していないため、汎用的に処理するなら
|
36
40
|
フォームのエレメンツを走査する必要があります
|
41
|
+
# 調整版
|
37
42
|
|
38
43
|
```
|
39
|
-
$(function () {
|
40
|
-
$("button").on('click', function (e) {
|
41
|
-
var fd=new FormData($(this).prop('form'));/*formdataはとりあえずおいておく*/
|
42
44
|
var f=$(this).prop('form').elements;
|
43
45
|
var n=[];
|
44
46
|
for(i=0;i<f.length;i++){
|
45
|
-
if(f.item(i).name
|
47
|
+
if(f.item(i).name && !f.item(i).disabled){
|
48
|
+
var ty=f.item(i).type;
|
49
|
+
if((ty=="radio" || ty=="checkbox") && f.item(i).checked ||
|
50
|
+
ty=="text" ||
|
51
|
+
ty=="hidden" ||
|
52
|
+
ty=="textarea" ||
|
53
|
+
ty=="select-one"
|
54
|
+
){
|
55
|
+
n.push([f.item(i).name,f.item(i).value]);
|
56
|
+
}
|
57
|
+
}
|
46
58
|
}
|
47
|
-
n=n.filter(function(i,j,k){return k.indexOf(i)==j;});
|
48
|
-
console.log(n);
|
59
|
+
console.log(n);
|
49
60
|
e.preventDefault();
|
50
61
|
});
|
51
62
|
});
|