0

次のようなhtmlテーブルがあります。

     Invoice#          Due Date      Balance      Select 
      12345           12/25/2011    $1,356.00       X
      56789           12/30/2011    $1,000.00       X

      Total:                        $2,356.00

テーブルの下にテキストボックスがあります。行のチェックボックスが選択または選択解除されたときに、テキストボックスの値を更新して、選択した行の残高を加算または減算します。つまり、チェックボックスがオフの場合は行に記載されている残高を減算し、チェックすると追加します. デフォルトでは、ボックスはチェックされています。

4

2 に答える 2

1

<p>それぞれに, <a>&のような他の子要素がないと仮定すると<button>、テキストだけが含まれます。<tr>そうでない場合は、要素に到達するまで最初のステートメントを繰り返します。

function getBalance(e){
  e = e.parentElement; //get the parent of the <td>.
  e = e.children[2]; //get the 3rd <td> of that <tr>.
  //finally copy the textContent to the value attribute.
  document.getElementById(inputboxId).setAttribute('value', e.textContent);
  //e.innerText for older browsers.
}

4 列目のセルに onclick イベント ハンドラを配置します。

<td onclick='getBalance(this)'>X</td>
于 2012-06-15T06:43:14.667 に答える
0

jQueryを想定:

$('table input[type="checkbox"]').click(function() {
    var curBalance=new Number($("#textbox").val());
    var rowBalance=new Number($(this).parent().prev().text());

    var newNum=0;
    if (this.checked) newBalance=curBalance+rowBalance;
    else newBalance=curBalance-rowBalance;

    $("#textbox").val(newBalance);
});
于 2012-06-15T06:18:15.157 に答える