0

私はJQueryを初めて使用し、問題が発生しました。チェックボックスがあるテーブル行から特定のセル値を読み取りたい。チェックボックスがオンになっているイベントを処理するイベントがあります。これは私のコードです:

$("#businesses input:checkbox").change(function (
        var $this = $(this);
        if ($this.is(":checked")) {

            //Here I want to read a value from a column in a row where is the checkbox

        } else {
            //Here I want to read a value from a column in a row where is the checkbox 
        }
});

「businesses」というテーブルがあり、この形式になっています

<table id="businesses">
<tr>
  <th>Select Value</th>

  <th>Value</th>
</tr>
<tr>
  <td><input type="checkbox" class="selectedService" title="Seleccionar" /></td>
  <td>125</td> 
</tr>  
<tr>
  <td><input type="checkbox" class="selectedService" title="Seleccionar" /></td>
  <td>126</td> 
</tr>
</table>

私がやりたいのは、チェックボックスを選択すると、その行の値フィールドを取得することです。

最初のチェックボックスを押すと、125を取得したいと思います。

ありがとう!!!

4

4 に答える 4

1

this(イベントハンドラー関数の)チェックボックスから始めて、含まれている<td>要素に移動し、次の<td>要素に移動して、そのテキストを取得する必要があります。

$('#businesses input:checkbox').change(function(e) {
    if(this.checked) {
        var value = parseInt($(this).closest('td').next('td').text(), 10);
        // above will convert it from a string to an integer
    }
    else {
        // same as above? Seems redundant
    }
});
于 2013-01-31T16:25:48.523 に答える
0

siblings()親の使用:

$this.closest('td').siblings().text();

これだけではない場合<td> siblings()は、残りのすべてが返されるため、適切なセレクターを使用してフィルターをかけます。

于 2013-01-31T16:26:07.097 に答える
0

あなたはそれにアクセスすることができます:

$this.parent().next().text();
于 2013-01-31T16:27:58.697 に答える
0

これを試して...

$("#businesses input:checkbox").on ('change', function (e)
        if ($(this).is(":checked")) {
            $( e.target ).closest("td").text();
        } else {
            //Here I want to read a value from a column in a row where is the checkbox 
        }
});

ご挨拶。

于 2013-01-31T16:34:06.733 に答える