0

HTMLに次の表があります。

<table class="dataTable" id="repaymentShedule">
                <thead>
                    <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>
                    </tr>
                </thead>
                <tbody data-bind="foreach: creditDetails">
                    <tr>
                        <td class="paymentDate" data-bind="text: dateOfPayment"></td>
                        <td class="startBalance" data-bind="text: beginingBalance"></td>                        
                        <td class="monthlyInt" data-bind="text: monthlyInterest"></td>
                        <td class="principal"><input data-bind="value: princpalPayment"></input></td>
                        <td class="monthlyInst" data-bind="text: monthlyInstallment"></td>
                        <td class="remainingBalance" data-bind="text: endingBalance"></td>
                        <td class="paid"><input type="checkbox" data-bind="checked: isPaid, disable: isPaid, click: testFunc, value: true"></td> <!-- value: true moje da ne e nujno -->
                        <td class="currentDate" data-bind="text: currentDate"></td>
                    </tr>
                </tbody>
            </table>

値はknockout jsバインディングから取得されます。principalそして、以下の関数を使用してクラスのすべての値を取得しようとしています:

updateValues = function(){
    $("tbody").find("tr").each(function() {                               
       var principal = $(this).find('td.principal').val();
       console.log(principal);
    });                           
};

Bu コンソールは次を返します。(an empty string)

編集: 上記の関数は、をpaymentDateに変更するだけで、クラスで問題なく動作.val()します.text()

正しい方法で値を取得していないこと、またはバインディングによって現在の値を取得できないことは確かですが、問題を特定することはできません

4

5 に答える 5

6

これを行う必要があります:

var principal = $(this).find('td.principal :input').val();

inputclass で表のセル内の要素の値を取得しますprincipal

また、.val() API ドキュメントに従って:-

.val() メソッドは主に、input、select、textarea などのフォーム要素の値を取得するために使用されます。要素の場合、.val() メソッドは、選択された各オプションを含む配列を返します。オプションが選択されていない場合は、null を返します。

したがって、コードの使用中にコンソールに空の文字列が表示されます。

上記の関数は、 .val() を .text() に変更するだけで、 paymentDate クラスで問題なく動作します

これは、テーブル セルの.val()を toに変更した後に正しい値が得られた理由も説明しています。これは、その中に要素が含まれていないためです。.text()paymentDateinput

于 2013-10-24T06:46:20.923 に答える
3
<td class="principal"><input data-bind="value: princpalPayment"></input></td>

要素は自己終了するため、終了</input>タグは必要ありません。input

そして、あなたはより良いターゲットを絞る必要があります。

var principal = $(this).find('td.principal input').val();
于 2013-10-24T06:46:26.557 に答える
1

your not looking for the input value your looking at the td value which clearly doesnt exist here is a jsfiddle with the fixed jquery

http://jsfiddle.net/krxva/

updateValues = function(){
$("tbody").find("tr").each(function() {                               
   var principal = $(this).find('td.principal').find('input').val();
   console.log(principal);
});                           
};
于 2013-10-24T06:53:42.903 に答える
1
var principal = $(this).find('td.principal').val();
           console.log(principal);

td入力要素もあるため、上記は機能しません。

その中に入力要素の値が必要tdなので、次のように使用します。

 var principal = $(this).find('td.principal input').val();
 // Or use this
 var principal = $(this).find('td.principal > input').val();
 console.log(principal);

理解のためにこれを参照してください

于 2013-10-24T06:59:40.483 に答える