質問するログイン新規登録

回答編集履歴

2

修正

2016/09/20 06:46

投稿

yambejp
yambejp

スコア118171

answer CHANGED
@@ -14,6 +14,8 @@
14
14
  ```
15
15
 
16
16
  #省略なし
17
+ ※load処理のためにchangeイベントに変更しました
18
+
17
19
  ```HTML
18
20
  <script src="jquery.js"></script>
19
21
  <script>
@@ -49,17 +51,20 @@
49
51
  }
50
52
  return true;
51
53
  });
52
- $('input[name = bbs]').click(function() {
54
+ $('input[name = bbs]').change(function() {
53
- var myid = $(this).prop('id');
55
+ var myid = $(this).prop('id');
54
- var sendlist = {
56
+ var sendlist = {
55
57
  "r1":{"text":"投稿",checkflg:true},
56
58
  "r2":{"text":"更新",checkflg:false},
57
59
  "r3":{"text":"削除",checkflg:false},
58
- };
60
+ };
61
+ if($(this).prop('checked')==true){
59
- $('#send').val(sendlist[myid].text);
62
+ $('#send').val(sendlist[myid].text);
60
- $('[name = "chkid[]"]').prop('disabled',sendlist[myid].checkflg);
63
+ $('[name = "chkid[]"]').prop('disabled',sendlist[myid].checkflg);
64
+ }
65
+ }).change();
66
+
61
67
  });
62
- });
63
68
  </script>
64
69
  </head>
65
70
  <body>
@@ -69,7 +74,7 @@
69
74
  <td>
70
75
  名前:<input type="text" name="name" id="name">
71
76
  内容:<textarea name="comment" cols="30" rows="3" id="comment"></textarea>
72
- <input type="radio" name="bbs" id="r1" value="post">投稿
77
+ <input type="radio" name="bbs" id="r1" value="post" checked>投稿
73
78
  <input type="radio" name="bbs" id="r2" value="update">更新
74
79
  <input type="radio" name="bbs" id="r3" value="delete">削除
75
80
  <input type="submit" id="send">
@@ -80,4 +85,6 @@
80
85
  <input type="checkbox" name="chkid[]">test1<br>
81
86
  <input type="checkbox" name="chkid[]">test2<br>
82
87
  <input type="checkbox" name="chkid[]">test3<br>
88
+
89
+
83
90
  ```

1

追記

2016/09/20 06:46

投稿

yambejp
yambejp

スコア118171

answer CHANGED
@@ -11,4 +11,73 @@
11
11
  $('#send').val(sendlist[myid].text);
12
12
  });
13
13
 
14
+ ```
15
+
16
+ #省略なし
17
+ ```HTML
18
+ <script src="jquery.js"></script>
19
+ <script>
20
+ $(document).ready(function() {
21
+ $('form').on('submit',function() {//フォーム入力時の条件分岐
22
+ if ($('#r1').prop('checked')) {
23
+ with($('#name')) {
24
+ if (val().length >= 10) {
25
+ window.alert("ERROR1");
26
+ return false;
27
+ }
28
+ if (val() === "") {
29
+ window.alert("ERROR2");
30
+ return false;
31
+ }
32
+ }
33
+ with($('#comment')) {
34
+ if (val().length >= 300) {
35
+ window.alert("ERROR3");
36
+ return false;
37
+ }
38
+ if (val() === "") {
39
+ window.alert("ERROR4");
40
+ return false;
41
+ }
42
+ }
43
+ }
44
+ if ($('#r2').prop('checked') || $('#r3').prop('checked')) {
45
+ if ($('[name = "chkid[]"]:checked').length == 0) {
46
+ window.alert("ERROR5");
47
+ return false;
48
+ }
49
+ }
50
+ return true;
51
+ });
52
+ $('input[name = bbs]').click(function() {
53
+ var myid = $(this).prop('id');
54
+ var sendlist = {
55
+ "r1":{"text":"投稿",checkflg:true},
56
+ "r2":{"text":"更新",checkflg:false},
57
+ "r3":{"text":"削除",checkflg:false},
58
+ };
59
+ $('#send').val(sendlist[myid].text);
60
+ $('[name = "chkid[]"]').prop('disabled',sendlist[myid].checkflg);
61
+ });
62
+ });
63
+ </script>
64
+ </head>
65
+ <body>
66
+ <form method="post" action="">
67
+ <table>
68
+ <tr>
69
+ <td>
70
+ 名前:<input type="text" name="name" id="name">
71
+ 内容:<textarea name="comment" cols="30" rows="3" id="comment"></textarea>
72
+ <input type="radio" name="bbs" id="r1" value="post">投稿
73
+ <input type="radio" name="bbs" id="r2" value="update">更新
74
+ <input type="radio" name="bbs" id="r3" value="delete">削除
75
+ <input type="submit" id="send">
76
+ </td>
77
+ </tr>
78
+ </table>
79
+
80
+ <input type="checkbox" name="chkid[]">test1<br>
81
+ <input type="checkbox" name="chkid[]">test2<br>
82
+ <input type="checkbox" name="chkid[]">test3<br>
14
83
  ```