6

変更する必要がある td の値を持つ Entity Framework 生成テーブルがあります。ラジオボタンのグループを挿入する必要があるため、行ごとにこれを行う必要がありますが、ボタンの各行には一意の名前が必要です。

基本的に、「レート」の id を持つ td のテキストの値に基づいて、ボタンのグループにチェックされた値を設定し、値を取得した後に既存のテキストを削除する必要があります。

私のテーブルは次のようになります。

<table id="IndexTable">
<tr>
    <th>CoffeeID </th>
    <th>Degree </th>
    <th>Weight </th>
    <th>Profile </th>
    <th>UsageInfo </th>
    <th>Comments </th>
    <th>AmbientTemp </th>
    <th>Rating </th>
    <th>WeightDeducted </th>
    <th>Selected </th>
    <th>ProfileID </th>
    <th>ProfileStart </th>
</tr>
<tr>
    <td>1 </td>
    <td>1 </td>
    <td>3 </td>
    <td>9 </td>
    <td>Filter Brew </td>
    <td>Perfecto!! </td>
    <td>55 </td>
    <td id="Rating">4.25 </td>
    <td>1 </td>
    <td>4 </td>
    <td>34 </td>
    <td>1 </td>

</tr>
<tr>
    <td>3 </td>
    <td>15 </td>
    <td>99 </td>
    <td>87 </td>
    <td>Espresso </td>
    <td>WTF </td>
    <td>99 </td>
    <td id="Rating">4.5 </td>
    <td>5 </td>
    <td>3 </td>
    <td>66 </td>
    <td>4 </td>

</tr>

そして、私のスクリプトは次のとおりです。

$("tr").each(function() { //get all rows in table
var str = $("text[id=Rating]").val(); //assign text with matching id to str ---doesn't work
$("td").each(function() { //get all tds in current row
    var newStr = ("#Rating").text; //assign text in td with id Rating to newStr ---- doesn't work
    alert(newStr); // fires undefined for each td in table? 
    //This is where I want to insert radio buttons and assign the one with the value of the Rating td to checked.
});
});

これは最新のものです。私はこのサイト全体を見てきましたが、特定の行の特定の列から値を取得し、値を変更し、各行/列に対して繰り返すことを扱っていません。テーブル。私はデータベースのバックグラウンドから来ましたが、これは本当に単純で、非常にイライラします。

4

3 に答える 3

9

SKS が述べているように、HTML ドキュメントと同じ ID を持つ複数のノードを HTML ドキュメントに含めることはできません。ID は一意である必要があります。代わりにクラスを使用することをお勧めします。

また、ヘッダー行を に<thead>、テーブルの本文を<tbody>

これで、jQuery で $("td")、行内のものだけでなく、ドキュメント内のすべての td が検索されます。

使用したい、 $(this).find('td')または .children または派手なより高いレベルのセレクターを使用できます。この情報を取得するには、さまざまな正しい方法があります。

私はあなたのコードを取り、次のように修正しました:

$("tbody").find("tr").each(function() { //get all rows in table

    var ratingTdText = $(this).find('td.Rating').text(); 
    //gets the text out of the rating td for this row
    alert(ratingTdText);

});

これは、わずかに変更された html で機能します。

<table id="IndexTable">
<thead>
    <tr>
    <th>CoffeeID </th>
    <th>Degree </th>
    <th>Weight </th>
    <th>Profile </th>
    <th>UsageInfo </th>
    <th>Comments </th>
    <th>AmbientTemp </th>
    <th>Rating </th>
    <th>WeightDeducted </th>
    <th>Selected </th>
    <th>ProfileID </th>
    <th>ProfileStart </th>
</tr>
</thead>
<tbody>
<tr>
    <td>1 </td>
    <td>1 </td>
    <td>3 </td>
    <td>9 </td>
    <td>Filter Brew </td>
    <td>Perfecto!! </td>
    <td>55 </td>
    <td class="Rating">4.25 </td>
    <td>1 </td>
    <td>4 </td>
    <td>34 </td>
    <td>1 </td>

</tr>
<tr>
    <td>3 </td>
    <td>15 </td>
    <td>99 </td>
    <td>87 </td>
    <td>Espresso </td>
    <td>WTF </td>
    <td>99 </td>
    <td class="Rating">4.5 </td>
    <td>5 </td>
    <td>3 </td>
    <td>66 </td>
    <td>4 </td>

</tr>
</tbody>
</table>

これが正しい方向に進むのに役立つことを願っています!

ああ、私はほとんど忘れていました!ここにjsfiddleへのリンクがあるので、これを実際に見ることができます:http://jsfiddle.net/fCpc6/

于 2012-04-09T19:04:00.380 に答える
1

私はあなたが探していると思います:

var newStr = ("#Rating").text();

または:

var newStr = ("td[id='Rating']").text();
于 2012-04-09T18:57:51.060 に答える
0

JavaScript にいくつかのエラーがあります。

  • 現在の に属するthisを取得するために使用します。tdtr
  • "text[id=Rating]"は無効なセレクタです - テキスト タグがありません。
  • #RatingID は一意でなければならないため、おそらくクラスである必要があります。( に変更<td id="Rating"><td class="rating">ます。
  • の括弧がありませんでした.text()

更新されたサンプルは次のとおりです。

$("tr").each(function() { //get all rows in table
  $("td", this).each(function() { //get all tds in current row
    var newStr = $(".rating").text(); //assign text in td with id Rating to newStr ---- doesn't work
    alert(newStr); // fires undefined for each td in table? 
    //This is where I want to insert radio buttons and assign the one with the value of the Rating td to checked.
  });
});
于 2012-04-09T19:00:19.850 に答える