<table class="table"> <tbody> <tr> <th>項目1</th> <th>項目2</th> <th>項目3</th> <th>項目4</th> <th>項目5</th> <th>項目6</th> <th>項目7</th> <th>項目8</th> <th>項目9</th> </tr> <tr> <td><span>13</span></td> <td>C1</td> <td>C2</td> <td>C3</td> <td>C4</td> <td>C5</td> <td>C6</td> <td><input type="button" value="B1" onclick="location.href='/hoge/13'" /></td> <td><input type="button" value="B2" onclick="location.href='/foo/13'" /></td> <td><input type="button" value="Pv" class="Pv" /></td> </tr> <tr> <td><span>12</span></td> <td>C1</td> <td>C2</td> <td>C3</td> <td>C4</td> <td>C5</td> <td>C6</td> <td><input type="button" value="B1" onclick="location.href='/hoge/12'" /></td> <td><input type="button" value="B2" onclick="location.href='/foo/12'" /></td> <td><input type="button" value="Pv" class="Pv" /></td> </tr> <tr> <td><span>11</span></td> <td>C1</td> <td>C2</td> <td>C3</td> <td>C4</td> <td>C5</td> <td>C6</td> <td><input type="button" value="B1" onclick="location.href='/hoge/11'" /></td> <td><input type="button" value="B2" onclick="location.href='/foo/11'" /></td> <td><input type="button" value="Pv" class="Pv" /></td> </tr> </tbody> </table>
というテーブルで各行のPvのClass が付与されたボタンを押すとindex番号を取得して、その行の先頭の番号を取得しようとしています。
$('.Pv').click(function(){ var index = $(".Pv").index(this); alert("index=" + index); var text = $("span:eq(" + index + ")").text(); alert("text=" + text); });
上手くいきません。
どこが悪いでしょうか????
ご指摘頂けば幸いです。
「PvのClass が付与されたボタン」とありますが、どのボタンにもclassは付与されていないようですが
「うまくできない」とは「何をしたときに」「どうなると思って」「どうなったのか」を、出ているエラーなどと併せて、具体的に記述されたほうが回答を得られやすいと思います。
間違えました。スミマセン修正します。
転記ミスでしょうか?と思ったのですが、付与すれば(または.previewとかに変えれば)想定通りに動きますね。やってみてください。
転記ミスですか?うーーーん・・・。動かなかったんですよね・・・。ちょっとやってみます。
あ、いえ、最初のコメントに続けて書いているうちに他のコメントが入ってしまったのでわかれてしまってます。すみません。ただ、正しく付与すれば動作しますよ。
回答3件
あなたの回答
tips
プレビュー