\r\n```\r\nをheadタグの中に入れ、\r\n\r\n```javascript\r\n\r\n```\r\nをbodyの最下部に入れます。","dateModified":"2016-03-12T08:58:50.274Z","datePublished":"2016-03-12T08:58:50.274Z","upvoteCount":1,"url":"https://teratail.com/questions/29624#reply-46387"},"suggestedAnswer":[{"@type":"Answer","text":"質問とはズレますし御存じとは思いますが念のため。\r\n```HTML\r\n\r\n\r\n\r\n\r\n\r\n
check01のここにテキストが入ります。
\r\n
\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n
check02のテキストが入ります。
\r\n
\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n
check03のテキストが入ります。
\r\n
\r\n \r\n```\r\nラジオボタンにすればCSSだけで動きますよ。","dateModified":"2016-03-12T09:53:01.898Z","datePublished":"2016-03-12T09:53:01.898Z","upvoteCount":1,"url":"https://teratail.com/questions/29624#reply-46397","comment":[{"@type":"Comment","text":"作りたいものを考えると、ラジオボタンの方が適切そうですよね。","datePublished":"2016-03-12T12:32:34.650Z","dateModified":"2016-03-12T12:32:34.650Z"}]},{"@type":"Answer","text":"どんぴしゃな回答がamaranthineさんから出ているのでjqueryを使わない方法で。\r\n```HTML\r\n\r\n```","dateModified":"2016-03-12T09:54:47.911Z","datePublished":"2016-03-12T09:38:15.541Z","upvoteCount":1,"url":"https://teratail.com/questions/29624#reply-46395","comment":[{"@type":"Comment","text":"jqueryを使わない方がかっちょいいなぁ。。。","datePublished":"2016-03-13T12:14:35.910Z","dateModified":"2016-03-13T12:14:35.910Z"},{"@type":"Comment","text":"こう見ると、jqueryは偉大ですね。\r\namaranthineさんのサンプルの方がシンプルで分かり易いですよね。","datePublished":"2016-03-13T12:25:09.646Z","dateModified":"2016-03-13T12:25:09.646Z"},{"@type":"Comment","text":"そうですね、jQueryはほんと偉大ですよね(これ何回思ったか分からない)。ただ、すぐにjQueryのコードが浮かんでしまって、JSの勉強が捗らないんですよ。。jQueryに頼らず書きたいものです。","datePublished":"2016-03-13T16:42:30.197Z","dateModified":"2016-03-13T16:42:30.197Z"},{"@type":"Comment","text":"返信が遅れて申し訳ありません。\r\njavascriptのみでも思っていた挙動になりました。\r\nコードを眺めているだけでもとても勉強になります。\r\n本当にありがとうございます。","datePublished":"2016-03-22T06:45:31.960Z","dateModified":"2016-03-22T06:45:31.960Z"}]},{"@type":"Answer","text":"ちょっとヒントを。\r\n```Javascript\r\ndocument.getElementById('check01').checked = true;\r\ndocument.getElementById('check02').checked = false;\r\ndocument.getElementById('check03').checked = false;\r\n```ってやるとチェックを入れた動作と同じになります。","dateModified":"2016-03-12T09:00:29.807Z","datePublished":"2016-03-12T08:55:28.133Z","upvoteCount":0,"url":"https://teratail.com/questions/29624#reply-46385","comment":[]}],"breadcrumb":{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https://teratail.com","name":"トップ"}},{"@type":"ListItem","position":2,"item":{"@id":"https://teratail.com/tags/CSS3","name":"CSS3に関する質問"}},{"@type":"ListItem","position":3,"item":{"@id":"https://teratail.com/questions/29624","name":"チェックボックスをラジオボタンの様に扱わせたい"}}]}}}
cssの擬似クラスである:checkedを使いコンテンツが非表示される仕組みを製作したのですが、ラジオボタンの様にcheck01をクリックして隠されたコンテンツを表示し、その状態でcheck02をクリックしたらcheck01のチェックが外れ、check01のコンテツが非表示になるという仕組みをjavascriptを使い行いたいです。
コードをどのように書けば実装されるでしょうか。
よろしくお願いします。
以下にアップロードしたサイトのURLとhtml、cssのソースコードを掲載しています。
http://www.geocities.jp/jyqwc878/check/index.html
html
<body>
<section>
<label for="check01">check01</label>
<input id="check01" type="checkbox">
<div class="panel">
<p>check01のここにテキストが入ります。</p>
</div>
</section>
<section>
<label for="check02">check02</label>
<input id="check02" type="checkbox">
<div class="panel">
<p>check02のテキストが入ります。</p>
</div>
</section>
<section>
<label for="check03">check03</label>
<input id="check03" type="checkbox">
<div class="panel">
<p>check03のテキストが入ります。</p>
</div>
</section>
</body>
css
input {
display: none;
}
label {
cursor: pointer;
display: inline-block;
padding: 10px 20px;
border-radius: 4px;
background: #0bd;
color: #FFF;
-webkit-transition: 0.1s;
transition: 0.1s;
}
label:hover {
background: #0090aa;
}
.panel {
-webkit-transition: .3s ease;
transition: .3s ease;
height: 0;
overflow: hidden;
background: #F5F0CF;
margin-top: 10px;
padding: 0;
border-radius: 5px;
}
input:checked + .panel {
height: auto;
padding: 15px;
}
質問とはズレますし御存じとは思いますが念のため。
HTML
1<section>
2<label for="check01">check01</label>
3<input id="check01" type="radio" name="check">
4
5<div class="panel">
6 <p>check01のここにテキストが入ります。</p>
7</div>
8</section>
9
10<section>
11<label for="check02">check02</label>
12<input id="check02" type="radio" name="check">
13
14<div class="panel">
15 <p>check02のテキストが入ります。</p>
16</div>
17</section>
18
19<section>
20<label for="check03">check03</label>
21<input id="check03" type="radio" name="check">
22
23<div class="panel">
24 <p>check03のテキストが入ります。</p>
25</div>
26</section>
ラジオボタンにすればCSSだけで動きますよ。
どんぴしゃな回答がamaranthineさんから出ているのでjqueryを使わない方法で。
HTML
1<script type="text/javascript">
2window.onload = function() {
3 var ar = Array.prototype.slice.call(document.getElementsByTagName('input'));
4 for(e in ar) {
5 ar[e].onchange = function() {
6 for(ee in ar) {
7 ar[ee].checked = false;
8 }
9 this.checked = true;
10 };
11 }
12};
13</script>
ベストアンサー
タイトルと質問が一致していないのか、それとも私が言いたいことを理解できていないのか不明ですが、
jqueryを使って、下記スクリプトで1をチェックしたら、2と3がチェックが外れ、2をチェックしたら、1と3のチェックが外れ…というのでしたら、実現できます。
html
1<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
をheadタグの中に入れ、
javascript
1<script>
2 $('input[type=checkbox]').change(function() {
3
4 $('input[type=checkbox]').prop('checked', false);
5 $(this).prop('checked', true);
6
7 });
8</script>
をbodyの最下部に入れます。
ちょっとヒントを。
Javascript
1document.getElementById('check01').checked = true;
2document.getElementById('check02').checked = false;
3document.getElementById('check03').checked = false;
4```ってやるとチェックを入れた動作と同じになります。
15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.29%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる
質問する
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/03/12 12:32