以下のような内容でnyuuryokuボタンが押されたらその行番号をパラメータとしてfncset_dataに渡したいです。
★部分に該当のボタンの行番号を入れるようにしたらよいのですが、
rowIndexや$(this).parent()[0]や$("tr").index(this)などいろいろと調べた結果を★部分へ入れても-1が入ってしまい行番号が出力されません。
どのようにしたら渡せますでしょうか?
環境はwindows10のIEとなります。
html
1<table border="0" cellspacing="0" cellpadding="0"> 2 <tr> 3 <th scope="col" class="f-weight" style="width:50px;"> </th> 4 <th scope="col" width="220px" class="f-weight">データ1</th> 5 <th scope="col" width="120px" class="f-weight">データ2</th> 6 </tr> 7 <tr> 8 <td style='width:50px;' align='center'> 9 <input name='nyuuryoku' type='button' id='nyuuryoku' onclick='fncset_data(★);' value='入力' /> </td> 10 <td>"._hs($CN->GetFieldData('aaaaa'))."</td> 11 <td>"._hs($CN->GetFieldData('bbbbb'))."</td> 12 </tr> 13</table>
上記にも記載しておりますが、試してみたコードは以下のような内容です。
このように$("table_id").index(this)や数パターン試してもfncset_dataには-1しか返ってきませんでした。
html
1<table id="table_id" border="0" cellspacing="0" cellpadding="0"> 2 <tr> 3 <th scope="col" class="f-weight" style="width:50px;"> </th> 4 <th scope="col" width="220px" class="f-weight">データ1</th> 5 <th scope="col" width="120px" class="f-weight">データ2</th> 6 </tr> 7 <tr> 8 <td style='width:50px;' align='center'> 9 <input name='nyuuryoku' type='button' id='nyuuryoku' onclick='fncset_data($("table_id").index(this));' value='入力' /> </td> 10 <td>"._hs($CN->GetFieldData('aaaaa'))."</td> 11 <td>"._hs($CN->GetFieldData('bbbbb'))."</td> 12 </tr> 13</table>
このようなパターンでも確認しています。
html1.php
1<table id="table_id" border="0" cellspacing="0" cellpadding="0"> 2 <tr> 3 <th scope="col" class="f-weight" style="width:50px;"> </th> 4 <th scope="col" width="220px" class="f-weight">データ1</th> 5 <th scope="col" width="120px" class="f-weight">データ2</th> 6 </tr> 7 <tr> 8 <td id='nyuuryoku' style='width:50px;' align='center'> 9 <input type='button' onclick='fncset_data(0);' value='入力' /> </td> 10 <td>"._hs($CN->GetFieldData('aaaaa'))."</td> 11 <td>"._hs($CN->GetFieldData('bbbbb'))."</td> 12 </tr> 13</table>
html2.php(html1.phpの呼び元)
1function fncset_data(data) { 2 3 try{ 4 alert($("td.nyuuryoku").index(this));★-1が表示される 5 }catch( e ){ 6 alert('err'); 7 } 8 9}
提案していただいたように、テーブルの呼び元で行数を取得するようにしました。
取得した行数を用いて、テーブルの値をtbl.rows[?].cells[?].innerText;などで取得します。
html1.php
<table id="table_id" border="0" cellspacing="0" cellpadding="0"> <tr> <th scope="col" class="f-weight" style="width:50px;"> </th> <th scope="col" width="220px" class="f-weight">データ1</th> <th scope="col" width="120px" class="f-weight">データ2</th> </tr> <tr> <td id='nyuuryoku' style='width:50px;' align='center'> <input type='button' value='入力' /> </td> <td>"._hs($CN->GetFieldData('aaaaa'))."</td> <td>"._hs($CN->GetFieldData('bbbbb'))."</td> </tr> </table>
html2.php(html1.phpの呼び元)
$(document).ready(function () { $("#table_id").click(function() { alert($("td.nyuuryoku").index(this)); var value = tbl.rows[?].cells[?].innerText; $("#data_name").val(value); }); });
・他にも試してみたこと
html2.phpのclick(function() のなかで以下を実施してみましたが、なぜかundefinedとエラーとなり行数が取得できませんでした。
var col = $("#table_id").cellIndex;
alert(col);
また、以下も同様でundefinedと出力されました。
alert('行:'+this.parentNode.rowIndex+'列:'+this.cellIndex)
・またまた試してみたこと
やはりうまくいかないのでhtml1.phpでonclickを使用し、onclick='fncset_data(this);'と記載しそれを受け取ったhtml2.phpのfncset_data内で受け取ったthisを使用し、以下のように処理してみましたが結果は-1,undefined,undefinedとなり行数は出力されませんでした。
同記載すればよいのでしょうか?
function fncset_data(data) { try{ alert($("#table_id").index(data)); alert('行:'+data.parentNode.rowIndex) alert('行:'+data.cellIndex) }catch( e ){ alert('err'); } }
回答2件
あなたの回答
tips
プレビュー