質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.40%
HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

jQuery

jQueryは、JavaScriptライブラリのひとつです。 簡単な記述で、JavaScriptコードを実行できるように設計されています。 2006年1月に、ジョン・レシグが発表しました。 jQueryは独特の記述法を用いており、機能のほとんどは「$関数」や「jQueryオブジェクト」のメソッドとして定義されています。

Q&A

2回答

361閲覧

jsを利用してラジオボタンとチェックボックスで選択しているものを特定の商品に加点方式に

退会済みユーザー

退会済みユーザー

総合スコア0

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

jQuery

jQueryは、JavaScriptライブラリのひとつです。 簡単な記述で、JavaScriptコードを実行できるように設計されています。 2006年1月に、ジョン・レシグが発表しました。 jQueryは独特の記述法を用いており、機能のほとんどは「$関数」や「jQueryオブジェクト」のメソッドとして定義されています。

0グッド

2クリップ

投稿2024/06/29 05:00

編集2024/06/30 02:21

実現したいこと

フォームのラジオボタンとチェックボックスを選択していき、表画像のように各商品にだけ0か1ずつ加点していき合計点数を出したい。
そして合計点数が高い商品2つを表示したい商品と紐づけてopenというクラスを付与したい。
合計点数を出すまではできたが、紐づける方法が全くわからない。
JS初心者でソースも汚いです。

イメージ説明

発生している問題・分からないこと

今の方法では配列の商品1とクラスの$("itemimg1")などの紐付けができない気がする。

該当のソースコード

html

1 <form action="" id="form"> 2 <div class="inner"> 3 <h1>質問1</h1> 4 <label for=""><input type="radio" id="qa01_01" name="qa01" value="11111">軽い</samp></label> 5 <label for=""><input type="radio" id="qa01_02" name="qa01" value="01111">重い</label> 6 </div> 7 <div class="inner"> 8 <h1>質問2</h1> 9 <label for="qa02_01"><input type="radio" id="qa02_01" name="qa02" value="00101">大きい</label> 10 <label for="qa02_02"><input type="radio" id="qa02_02" name="qa02" value="10000">小さい</label> 11 </div> 12 <div class="inner"> 13 <h1>質問3</h1> 14 <label for="qa03_01"><input type="radio" id="qa03_01" name="qa03" value="00011">硬い</label> 15 <label for="qa03_02"><input type="radio" id="qa03_02" name="qa03" value="11100">柔らかい</label> 16 <label for="qa03_03"><input type="radio" id="qa03_03" name="qa03" value="00011">普通</label> 17 </div> 18 <div class="inner"> 19 <h1>質問4</h1> 20 <label for="qa04_01"><input type="checkbox" id="qa04_01" name="qa04" value="01011"/></label> 21 <label for="qa04_02"><input type="checkbox" id="qa04_02" name="qa04" value="10001"/></label> 22 <label for="qa04_03"><input type="checkbox" id="qa04_03" name="qa04" value="00010"/>黄色</label> 23 </div> 24 <input type="button" class="btn" value="送信する"> 25 </form> 26 <div class="itemimg1">item01</div> 27 <div class="itemimg2">item02</div> 28 <div class="itemimg3">item03</div> 29 <div class="itemimg4">item04</div> 30 <div class="itemimg5">item05</div>

jQuery

1var item = ['商品1' , '商品2' , '商品3' , '商品4' , '商品5']; 2item[0] = $(".itemimg1"); 3item[1] = $(".itemimg2"); 4item[2] = $(".itemimg3"); 5item[3] = $(".itemimg4"); 6item[4] = $(".itemimg5"); 7 8$('input:button').click(function() { 9 10var qa01 = $('input[name="qa01"]:checked').val(); 11var qa02 = $('input[name="qa02"]:checked').val(); 12var qa03 = $('input[name="qa03"]:checked').val(); 13var qa04 = $('input[name="qa04"]:checked').val(); 14 15for (let i = 0; i < 5; i++ ) { 16 item[i] = Number(qa01[i]) + Number(qa02[i]) + Number(qa03[i]) + Number(qa04[i]); 17 console.log(item[i]); 18} 19 20 21function compareFunc(a, b) { 22 return b - a; 23} 24newitem = item.sort(compareFunc); 25console.log(newitem);

試したこと・調べたこと

  • teratailやGoogle等で検索した
  • ソースコードを自分なりに変更した
  • 知人に聞いた
  • その他
上記の詳細・結果

ifを試してり、色々してみたができる気がしない

補足

特になし

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Lhankor_Mhy

2024/06/29 05:38

もう少し詳しくお願いします。たとえば、「表の特定の商品」をどうやって特定するのか、など。
退会済みユーザー

退会済みユーザー

2024/06/29 07:17

説明不足ですいません。 特定の商品というか、 例えば質問1の「軽い」を選択したら全商品に0か1を割り当てるという認識です。
Lhankor_Mhy

2024/06/29 07:31 編集

もう少し詳しくお願いします。 たとえば、「軽い」を選択したときに「商品1」行の「軽い」列には、どういうルールに基づいて0と1のどちらを割り当てるのか、などを教えてください。(サンプルでは1が割り当てられているようです)
guest

回答2

0

元のコードを生かすならこんな感じではないでしょうか。

js

1//... 2 3for (let i = 0; i < 5; i++) { 4 item[i].data('test', Number(qa01[i]) + Number(qa02[i]) + Number(qa03[i]) + Number(qa04[i])); 5 console.log(item[i]); 6} 7 8 9function compareFunc(a, b) { 10 return b.data('test') - a.data('test'); 11} 12newitem = item.sort(compareFunc); 13console.log(newitem); 14item.forEach(x => x.hide()); 15item[0].show(); 16item[1].show();

投稿2024/07/01 05:00

Lhankor_Mhy

総合スコア36601

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ちょっと曖昧なので一旦こちらをご確認ください
想定している動作と違うようでしたら具体的に希望を明示ください

html

1<style> 2.open{background-Color:yellow;} 3</style> 4 5<script> 6document.addEventListener('change',()=>{ 7 ['goods1','goods2','goods3','goods4','goods5'].reduce((x,y)=>{ 8 const l=document.querySelectorAll(`tbody .${y}:checked`).length; 9 document.querySelector(`tfoot .${y}`).textContent=l; 10 x.push([y,l]); 11 return x; 12 },[]).sort((x,y)=>y[1]-x[1]).forEach((x,y)=>{ 13 document.querySelector(`thead .${x[0]}`).classList.toggle('open',y<2); 14 }); 15}); 16</script> 17<table border> 18<thead> 19<tr> 20<th>&nbsp;</th> 21<th>&nbsp;</th> 22<th class="goods1">商品1</th> 23<th class="goods2">商品2</th> 24<th class="goods3">商品3</th> 25<th class="goods4">商品4</th> 26<th class="goods5">商品5</th> 27</tr> 28</thead> 29<tbody> 30<tr> 31<td rowspan="2">質問1</td> 32<td>軽い</td> 33<td><input type="checkbox" class="goods1"></td> 34<td><input type="checkbox" class="goods2"></td> 35<td><input type="checkbox" class="goods3"></td> 36<td><input type="checkbox" class="goods4"></td> 37<td><input type="checkbox" class="goods5"></td> 38</tr> 39<tr> 40<td>重い</td> 41<td><input type="checkbox" class="goods1"></td> 42<td><input type="checkbox" class="goods2"></td> 43<td><input type="checkbox" class="goods3"></td> 44<td><input type="checkbox" class="goods4"></td> 45<td><input type="checkbox" class="goods5"></td> 46</tr> 47<tr> 48<td rowspan="2">質問2</td> 49<td>大きい</td> 50<td><input type="checkbox" class="goods1"></td> 51<td><input type="checkbox" class="goods2"></td> 52<td><input type="checkbox" class="goods3"></td> 53<td><input type="checkbox" class="goods4"></td> 54<td><input type="checkbox" class="goods5"></td> 55</tr> 56<tr> 57<td>小さい</td> 58<td><input type="checkbox" class="goods1"></td> 59<td><input type="checkbox" class="goods2"></td> 60<td><input type="checkbox" class="goods3"></td> 61<td><input type="checkbox" class="goods4"></td> 62<td><input type="checkbox" class="goods5"></td> 63</tr> 64<tr> 65<td rowspan="3">質問3</td> 66<td>硬い</td> 67<td><input type="checkbox" class="goods1"></td> 68<td><input type="checkbox" class="goods2"></td> 69<td><input type="checkbox" class="goods3"></td> 70<td><input type="checkbox" class="goods4"></td> 71<td><input type="checkbox" class="goods5"></td> 72</tr> 73<tr> 74<td>柔らかい</td> 75<td><input type="checkbox" class="goods1"></td> 76<td><input type="checkbox" class="goods2"></td> 77<td><input type="checkbox" class="goods3"></td> 78<td><input type="checkbox" class="goods4"></td> 79<td><input type="checkbox" class="goods5"></td> 80</tr> 81<tr> 82<td>普通</td> 83<td><input type="checkbox" class="goods1"></td> 84<td><input type="checkbox" class="goods2"></td> 85<td><input type="checkbox" class="goods3"></td> 86<td><input type="checkbox" class="goods4"></td> 87<td><input type="checkbox" class="goods5"></td> 88</tr> 89<tr> 90<td rowspan="3">質問4</td> 91<td></td> 92<td><input type="checkbox" class="goods1"></td> 93<td><input type="checkbox" class="goods2"></td> 94<td><input type="checkbox" class="goods3"></td> 95<td><input type="checkbox" class="goods4"></td> 96<td><input type="checkbox" class="goods5"></td> 97</tr> 98<tr> 99<td></td> 100<td><input type="checkbox" class="goods1"></td> 101<td><input type="checkbox" class="goods2"></td> 102<td><input type="checkbox" class="goods3"></td> 103<td><input type="checkbox" class="goods4"></td> 104<td><input type="checkbox" class="goods5"></td> 105</tr> 106<tr> 107<td>黄色</td> 108<td><input type="checkbox" class="goods1"></td> 109<td><input type="checkbox" class="goods2"></td> 110<td><input type="checkbox" class="goods3"></td> 111<td><input type="checkbox" class="goods4"></td> 112<td><input type="checkbox" class="goods5"></td> 113</tr> 114</tbody> 115<tfoot> 116<tr> 117<th colspan="2">合計点数</th> 118<th><span class="goods1">0</span></th> 119<th><span class="goods2">0</span></th> 120<th><span class="goods3">0</span></th> 121<th><span class="goods4">0</span></th> 122<th><span class="goods5">0</span></th> 123</tr> 124</tfoot> 125</table>

投稿2024/07/01 01:18

yambejp

総合スコア115870

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.40%

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

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問