我想加上或减去1,一个简单的量的输入框,可以像下面的图片不同:使用JavaScript的onclick在一段PHP循环
PHP:
$query = ...
while ($row= mysqli_fetch_array($query))
{
<input type="hidden" value="<?php echo $row['id']; ?>" id="id_part">
<input type="text" value="<?php echo $row['quantity']; ?>" id="<?php echo $row['id']; ?>_count"><br>
<input type="button" value="-" id="minus" onclick="minus()">
<input type="button" value="+" id="plus" onclick="plus()">
}
的Javascript:
<script>
var count = 1;
var id = document.getElementById("id_part").value;
var countEl = document.getElementById(id + "_count");
function plus(){
count++;
countEl.value = count;
}
function minus(){
if (count > 1) {
count--;
countEl.value = count;
}
}
</script>
它不起作用。它只需要我第一个ID数量框。无法管理编辑第二个数量框,其中id应该不同。正如你所看到的,我为每个id标签分配了不同的名字,取自数据库id。
您正在重新使用'id'值,这在HTML中无效。使用唯一的'id'值。 – David
为什么不使用输入类型编号进行此行为? [(doc,滚动到输入类型编号 )](https://www.w3schools.com/html/html_form_input_types.asp)[(example)](https://www.w3schools.com/html/tryit.asp ?filename = tryhtml_input_number) –
我不喜欢那个输入的方面。 –