質問編集履歴
1
質問内容の変更
    
        title	
    CHANGED
    
    | 
         @@ -1,1 +1,1 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            Rails6  
     | 
| 
      
 1 
     | 
    
         
            +
            Rails6 jsファイルの読み込み
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -1,9 +1,64 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            rails初心者です。
         
     | 
| 
      
 2 
     | 
    
         
            +
            ビューへのjsファイルの適用の練習として
         
     | 
| 
       1 
     | 
    
         
            -
             
     | 
| 
      
 3 
     | 
    
         
            +
            1.各ビューにjavascript_pack_tagを記述
         
     | 
| 
       2 
     | 
    
         
            -
             
     | 
| 
      
 4 
     | 
    
         
            +
            2.application.jsにimport "./test.js"
         
     | 
| 
       3 
     | 
    
         
            -
             
     | 
| 
      
 5 
     | 
    
         
            +
            の2つを試しました。
         
     | 
| 
       4 
6 
     | 
    
         | 
| 
       5 
     | 
    
         
            -
             
     | 
| 
      
 7 
     | 
    
         
            +
            しかし、1では正しくjsファイルが読み込まれ、機能していましたが、2では計算ボタンを押しても何も反応が起こりません。
         
     | 
| 
      
 8 
     | 
    
         
            +
            今回使用したjsファイル(test.js)及びhtml.erbファイルは以下です。
         
     | 
| 
      
 9 
     | 
    
         
            +
            原因・解決策が分かる方、ご教示いただけますと幸いです。
         
     | 
| 
       6 
10 
     | 
    
         | 
| 
      
 11 
     | 
    
         
            +
            ```test.js
         
     | 
| 
      
 12 
     | 
    
         
            +
            let bmiform = document.getElementById("bmiIndex");
         
     | 
| 
      
 13 
     | 
    
         
            +
            let comment = document.getElementById("evil");
         
     | 
| 
       7 
     | 
    
         
            -
             
     | 
| 
      
 14 
     | 
    
         
            +
            const calcbtn = document.getElementById("calcbtn");
         
     | 
| 
       8 
15 
     | 
    
         | 
| 
      
 16 
     | 
    
         
            +
            function BMIcalc(){
         
     | 
| 
      
 17 
     | 
    
         
            +
                let $h = document.getElementById("hform");
         
     | 
| 
      
 18 
     | 
    
         
            +
                let $w = document.getElementById("wform");
         
     | 
| 
      
 19 
     | 
    
         
            +
                let h = $h.value;
         
     | 
| 
      
 20 
     | 
    
         
            +
                let w = $w.value;
         
     | 
| 
      
 21 
     | 
    
         
            +
                let Th = Number(h)/100;
         
     | 
| 
      
 22 
     | 
    
         
            +
                let bmires = Number(w)/(Th*Th);
         
     | 
| 
      
 23 
     | 
    
         
            +
                let BMI = bmires.toFixed(2);
         
     | 
| 
      
 24 
     | 
    
         
            +
                bmiform.textContent = BMI;
         
     | 
| 
      
 25 
     | 
    
         
            +
                if(BMI >= 30){
         
     | 
| 
      
 26 
     | 
    
         
            +
                    comment.textContent = "☆肥満☆";
         
     | 
| 
      
 27 
     | 
    
         
            +
                    comment.className = "fat";
         
     | 
| 
      
 28 
     | 
    
         
            +
                }
         
     | 
| 
      
 29 
     | 
    
         
            +
                else if(BMI < 30 && BMI >= 22){
         
     | 
| 
      
 30 
     | 
    
         
            +
                    comment.textContent = "普通";
         
     | 
| 
      
 31 
     | 
    
         
            +
                    comment.className = "medium";
         
     | 
| 
      
 32 
     | 
    
         
            +
                }
         
     | 
| 
      
 33 
     | 
    
         
            +
                else{
         
     | 
| 
      
 34 
     | 
    
         
            +
                    comment.textContent = "痩せ";
         
     | 
| 
      
 35 
     | 
    
         
            +
                    comment.className = "smart";
         
     | 
| 
      
 36 
     | 
    
         
            +
                }
         
     | 
| 
      
 37 
     | 
    
         
            +
            }
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
            calcbtn.addEventListener("click", BMIcalc);
         
     | 
| 
      
 40 
     | 
    
         
            +
            ```
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
            ```test.html.erb
         
     | 
| 
      
 43 
     | 
    
         
            +
            <div class="wrapper">
         
     | 
| 
      
 44 
     | 
    
         
            +
                <h2>
         
     | 
| 
      
 45 
     | 
    
         
            +
                    BMI自動計算機
         
     | 
| 
      
 46 
     | 
    
         
            +
                </h2>
         
     | 
| 
      
 47 
     | 
    
         
            +
                <div id="flame_1">
         
     | 
| 
      
 48 
     | 
    
         
            +
                    <div id="height">身長</div>
         
     | 
| 
      
 49 
     | 
    
         
            +
                    <input type="text" id="hform" value="">cm
         
     | 
| 
      
 50 
     | 
    
         
            +
                </div>
         
     | 
| 
      
 51 
     | 
    
         
            +
                <div id="flame_2">
         
     | 
| 
      
 52 
     | 
    
         
            +
                    <div id="weight">体重</div>
         
     | 
| 
      
 53 
     | 
    
         
            +
                    <input type="text" id="wform" value="">kg
         
     | 
| 
      
 54 
     | 
    
         
            +
                </div>
         
     | 
| 
      
 55 
     | 
    
         
            +
                <div id="calc">
         
     | 
| 
      
 56 
     | 
    
         
            +
                    <button type="button" id="calcbtn">計算</button>
         
     | 
| 
      
 57 
     | 
    
         
            +
                </div>
         
     | 
| 
      
 58 
     | 
    
         
            +
                <div id="result">
         
     | 
| 
       9 
     | 
    
         
            -
             
     | 
| 
      
 59 
     | 
    
         
            +
                    <span> BMI:</span><span id="bmiIndex"></span>
         
     | 
| 
      
 60 
     | 
    
         
            +
                </div>
         
     | 
| 
      
 61 
     | 
    
         
            +
                <div id="evil" class="">
         
     | 
| 
      
 62 
     | 
    
         
            +
                </div>
         
     | 
| 
      
 63 
     | 
    
         
            +
            </div>
         
     | 
| 
      
 64 
     | 
    
         
            +
            ```
         
     |