-2

データ行を持つテーブルがあります。

<tbody>                       
   <tr class="rows">
     <td><input type="text" name="total" value="3500.00" id="total" class="price"></td>
     <td>$<input type="text" value="23.00" name="customerS" id="customerS" class=""></td>
   </tr>

  <tr class="rows">
     <td><input type="text" name="total" value="3900.00" id="total" class="price"></td>
     <td>$<input type="text" value="3446.00" name="customerS" id="customerS" class=""></td>
  </tr>                            
</tbody>

行の ID を使用して関数を呼び出したい。

format('total', 'customerS');

私が理解できないのは.each、フォーマットが各tr(行)に対して呼び出されるように、関数フォーマットを呼び出す方法です。教えてください、ありがとう。

4

4 に答える 4

3

チェーンするだけeach()

$('tr.rows').each(function(){
    format('total', 'customers');
});

ただし、引数が要素のプロパティに依存する変数である必要がある場合は、tr詳細を提供する必要があります。

ただし、無効な HTML である sid内に含まれる要素で s を複製していることに注意してください。tr

于 2013-10-10T09:20:25.163 に答える
1

複数の要素に同じ ID を使用しないでください。代わりにクラスを使用できます。

      <tbody>                       
             <tr class="rows">
                 <td><input type="text" name="total" value="3500.00" id="total"   class="price">
                 </td>
                 <td>$<input type="text" value="23.00" name="customerS" class="customerS" class=""></td>
             </tr>

             <tr class="rows">
                 <td><input type="text" name="total" value="3900.00" id="total"   class="price">
                 </td>
                 <td>$<input type="text" value="3446.00" name="customerS" class="customerS" class=""></td>
             </tr>                            
                </tbody>

その後

jQuery(".customerS").each(function(index, element) {
    blah blah...
});
于 2013-10-10T09:23:47.307 に答える
1

これを試して

$(".rows").each(function(element){
var curretnRow = $(element);    
// apply your function here
});

$(".rows")=> これにより、クラス名が「Row」のすべての TR が取得されます。

.each(function(element){}=>これはコレクション内の各要素を反復処理し、「要素」変数の現在の要素への参照を提供します。

注:各 TR には同じ ID を保持する要素があることがわかります。繰り返し要素を処理する場合、これは正しい方法ではありません。クラス名を扱い、各要素に一意の ID を持たせることは、保守可能なコードを作成するための最初のステップであるべきだと思います。

これで問題が解決したかどうかをお知らせください。

于 2013-10-10T09:21:00.737 に答える
1

次のようにできます。

$(this).closest('tr').each(function() {...code...});
于 2013-10-10T09:35:32.053 に答える