phpでループ処理を行っている部分のクリック処理についてどのようにしたら良いでしょうか?よろしくお願いいたします。
###前提・実現したいこと
PHP,Javascript,Jqueryを使用してビンゴゲームを作ろうとしています。
動きの仕様としては、
・ランダムで数字が表示される
・表示された数字がシートにある時、そのマス(<td>)をクリックすると灰色に変わる
という感じです。
仕様の1つ目はできています。
###発生している問題・エラーメッセージ
表示されているシートのマス(<td>タグ)をクリックすると「Uncaught TypeError: Cannot read property 'on' of null」と値がちゃんとはいっていない?みたいで処理がうまくいきませんでした。
###該当のソースコード
index.php
<?php require_once(__DIR__ . '/config.php'); require_once(__DIR__ . '/Bingo.php'); $bingo = new \MyApp\Bingo(); $nums = $bingo->create(); ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="euc-jp"> <title>BINGO!</title> <link rel="stylesheet" href="css/styles.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script type="text/javascript" src="js/bingo.js"></script> </head> <body> <div id="container"> <table id="sheet"> <tr> <th>B</th><th>I</th><th>N</th><th>G</th><th>O</th> </tr> <?php for($i=0;$i<5;$i++){?> <tr> <?php for($j=0;$j<5;$j++){ ?> <td class="cols" onclick="colset()"><?= h($nums[$j][$i]); ?></td> <?php }?> </tr> <?php }?> </table><br> <section style="text-align:center;"> <input type="button" id="start" name="start" value="スタート" onclick="startBingo()"> <input type="button" id="stop" name="stop" value="ストップ" onclick="stopBingo()" style="display:none;"> <br> <div id="view" style="text-align:center;"></div> <hr> <div id="out"></div> <hr> </section> </div> <script type="text/javascript"> function colset(){ $('#sheet td').on('click', function(){ var col = $(this).text(); console.log(col); }); } </script> </body> </html>
Bingo.php
<?php namespace MyApp; class Bingo{ public function create(){ $nums = []; for($i = 0; $i <5; $i++){ $col = range($i * 15 + 1 , $i * 15 +15); shuffle($col); $nums[$i] = array_splice($col,0,5); } $nums[2][2] = "FREE"; return $nums; } } ?>
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/06 04:40
2016/10/06 04:46
2016/10/06 05:02
2016/10/06 05:04
2016/10/06 05:08