2015-09-05 60 views
1

我有一些数据库的结果,我在表格填充他们,我有这样的代码:我想要更新的特定文本框的值

<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='-'/> 

所以我想,当用户按下“定量”按钮更新特定行:

function addQty() { 
    document.getElementById("quant").value++; 
} 

function subtractQty() { 
    if (document.getElementById("quant").value - 1 < 0) { 
     return; 
    } else { 
     document.getElementById("quant").value--; 
    } 
    updateQuantity(); 
} 

此代码的工作时,我有一个排,当我有2个或更多的行没有什么工作,所以我可能不得不用这个词什么的?

+0

你能在jsfiddle.net上设置演示吗? –

+0

我如何更新那里的MySQL结果? – perastikos1

+0

您可以在那里给出示例数据。 –

回答

0

好吧我没有你的代码足够好的例子,但是这应该是足以让你在正确的方向...

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; 
 

 

 
    } 
 

 
}
<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>

+0

它的工作原理,非常感谢 – perastikos1

+0

@ perastikos1没有问题。请将其标记为正确的答案 – kurt

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='-' />