我想更新特定的文本框值

时间:2015-09-05 13:52:40

标签: javascript html

我有一些数据库结果,我填写表格,我有这个代码:

#Specify the number of n for the linear program.
param n := 5000;

#This is the set of probabilities of 
set N := {1 .. n};
#We specify the variables for the probabilities p_1,...p_n.
var p[<i> in N] real >= 0; 

#These are the values of the vector c. It specifies a constant for each p_i.
param c[<i> in N] := i/n ;

#We define the entries a_{ij} of the Matrix A.
defnumb a(i,j) := 
            if i < j then 0 
            else if i == j then i 
            else 1 end end;

#The objective function.
maximize prob: sum <i> in N : c[i] * p[i];

#The condition which needs to be fulfilled.
 subto condition:
    forall <i> in N:
      sum <j> in N: a(i,j) * p[j] <= 1;

所以我想在用户按下“quant”按钮时更新特定的行:

<input type='button' name='add' onclick='javascript: addQty();' value='+'/>
<span><?php echo $records['ct_qty']; ?></span>
<input type="text" class="gridder_input" name="quant[]" class="quant" id="quant[]" />
<input type='button' name='subtract' onclick='javascript: subtractQty();' value='-'/>

当我有一行时,当我有2行或更多行无效时,此代码有效,所以我可能不得不使用这个词或什么?

2 个答案:

答案 0 :(得分:1)

您可以使用同级选择器定位neare输入。

function addQty(elm) {
  elm.nextElementSibling.nextElementSibling.value++;
}

function subtractQty(elm) {
  if (elm.previousElementSibling.value - 1 < 0) {
    return;
  } else {
    elm.previousElementSibling.value--;
  }
  updateQuantity();
}
<input type='button' name='add' onclick='javascript: addQty(this);' value='+' />
<span><?php echo $records['ct_qty']; ?></span>
<input type="text" class="gridder_input" name="quant[]" class="quant" id="quant[]" />
<input type='button' name='subtract' onclick='javascript: subtractQty(this);' value='-' />

答案 1 :(得分:0)

好的我没有足够好的代码示例,但这足以让你朝着正确的方向前进......

&#13;
&#13;
function getTotals() {
  var table = document.getElementById('mytable');
  for (i = 0; i < table.rows.length; i++) {
    var quant = table.rows[i].querySelector('input[name="quant"]').value;
    var priceoriginal = table.rows[i].querySelector('input[name="priceoriginal"]').value;
    table.rows[i].querySelector('input[name="total"]').value = quant * priceoriginal;


  }

}
&#13;
<table id="mytable">
  <tr>
    <td>
      <input name="quant" type="text" value="2">
    </td>
    <td>
      <input name="priceoriginal" type="text" value="6">
    </td>
    <td>Total:
      <input name="total" type="text">
    </td>
  </tr>
  <tr>
    <td>
      <input name="quant" type="text" value="8">
    </td>
    <td>
      <input name="priceoriginal" type="text" value="4">
    </td>
    <td>Total:
      <input name="total" type="text">
    </td>
  </tr>
  <tr>
    <td>
      <input name="quant" type="text" value="5">
    </td>
    <td>
      <input name="priceoriginal" type="text" value="3">
    </td>
    <td>Total:
      <input name="total" type="text">
    </td>
  </tr>
</table>
<button onclick="getTotals()">Calculate Totals</button>
&#13;
&#13;
&#13;