我的html输入标记采用数组。我无法从javascript检索它们 顺序
以下内容根据所选复选框计算总额,并给出一个确认框。显示总额。无论此总额显示为0我的html输入标记采用数组。我无法从javascript检索它们 顺序,javascript,Javascript,以下内容根据所选复选框计算总额,并给出一个确认框。显示总额。无论此总额显示为0 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Order&l
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Order</title>
函数验证()
{
变量x,y,i,j,p=0;
j=parseInt(document.getElementById('loopcount').value);
文件。编写(j);
对于(i=1;i
这意味着name=“itemCode[]”而不是name=“itemCode[任何东西]”
试试这个:
name="itemCode[]"
如果您仍在努力理解,请在浏览器中使用firebug或类似工具检查输出的html。您为所有输入提供了相同的名称文档。getElementsByName()返回一个数组,而不是单个元素。
尝试以下操作:document.getElementsByName('itemCode[])[i]。已选中。
然后以0开始循环
document.getElementById('itemCode['+i+']')
for(i=0;i我的答案有点冗长,所以这里有一个关于您实际问题的更详细的答案:我在代码行旁边放了一些注释,我认为这会引起您的困惑
for (i=0;i<j;i++)
{
document.write(i);
if(document.getElementsByName('itemCode[]')[i].checked==true){
x=parseFloat( document.getElementsByName('quantity[]')[i].value);
document.write(x);
y=parseFloat( document.getElementsByName('price[]')[i].value);
document.write(y);
p+=x*y;
}
}
函数验证()
{
变量x,y,i,j,p=0;
j=parseInt(document.getElementById('loopcount').value);
document.write('j='+j);//使调试消息更有意义-S
对于(i=1;i
等等
您的代码中存在大量错误。请更新您的代码以消除我指出的问题,然后我们可能会取得一些进展。请不要在显然不属于代码一部分的普通英语句子前面放//OK抱歉,下次请记住这一点。我已尝试命名输入元素itemCode[]我尝试使用getElementById进行检索。但是Id必须是唯一的,而且我不知道如何在使用数组时为checkbox元素指定Id。请您帮忙。我是新手programming@user1744400:你真的需要在方括号之间放一些东西才能让它工作。我已经包括了一个例子循环不超过0。我有语句document.write(i)在我的代码中它只打印0。上面是我修改的代码。您看到的可能是docuemnt.write(j)的输出。for循环结构是正确的,这意味着边界条件是错误的。将documnet.write(j)替换为document.write('j='+j);你会明白我的意思。如果你正确使用它,它会工作。你会遇到什么样的错误?你甚至有javascript调试器吗?
<td><input
type="checkbox"
name="itemCode[<?php echo $row['itemCode']; ?>]"
etc etc
</td>
<td><input
type="checkbox"
id="itemCode[<?php echo $row['itemCode']; ?>]"
etc etc
</td>
document.getElementById('itemCode['+i+']')
for (i=0;i<j;i++)
{
document.write(i);
if(document.getElementsByName('itemCode[]')[i].checked==true){
x=parseFloat( document.getElementsByName('quantity[]')[i].value);
document.write(x);
y=parseFloat( document.getElementsByName('price[]')[i].value);
document.write(y);
p+=x*y;
}
}
<script type="text/javascript">
function validate()
{
var x,y,i,j,p=0;
j=parseInt(document.getElementById('loopcount').value);
document.write('j='+j); // make your debugging messages more meaningful -S
for (i=1;i<=j;i++)
{
document.write('i='+i); // make your debugging messages more meaningful -S
if(document.getElementsByName('itemCode['+i+']').checked==true)
{
//getElementsByName returns an array: replace them with getElementByName -S
x=parseFloat( document.getElementByName('quantity['+i+']').value);
document.write('x='+x); // make your debugging messages more meaningful
y=parseFloat( document.getElementByName('price['+i+']').value);
document.write('y='+y); // make your debugging messages more meaningful
p+=x*y;
}
}
var conf=confirm("The total amount is:"+ p);
if(conf==true){
header("Location:userHome.php");
}
else{
header("Location:orderveggies.php");
}
}
</script>
</head>
blah blah blah
while($row= mysqli_fetch_array($result)){?>
<tr>
<td>
<input
type="checkbox"
name="itemCode[<?php echo $row['itemCode']; ?>]" //Do this. seriously otherwise everything will have the same name -S
value="<?php echo $row['itemCode']; ?>"/>
<?php echo $row['itemCode']; ?>
</td>
<td>
<?php echo $row['itemName']; ?>
</td>
<td>
<input
type="hidden"
name="price[<?php echo $row['itemCode']; ?>]" //Im assuming here you want to know what price belongs to what item -S
value="<?php echo $row['price']; ?>"/>
<?php echo $row['price']; ?>
</td>
etc etc