通过javascript提交不会发送文本框值

时间:2011-07-27 15:23:27

标签: javascript post submit

有趣的问题。我有一个客户希望添加到购物车按钮是一个图像。没关系,通过javascript在图片上点击提交。但是当我查看发送的值时,数量总是1(或者我在代码中设置的任何值)。换句话说,我无法改变数量的价值。

<form name="AddToCartForm<?=$index?>" id="AddToCartForm<?=$index?>" action="[cartaddress]" method="post">
    <input type="hidden" value="1" name="insert" />
    <input type="hidden" value="<?=$subcategory['Name'];?>" name="category" />
    <input type="hidden" value="<?=$item['ProductNumber'];?>" name="prodnumber" />
    <input type="hidden" value="<?=$item['Name'];?>" name="prodname" />
    <input type="hidden" value="<?=$item['Price'];?>" name="prodprice" />
    <input type="hidden" value=".10" name="handling"/>
    <input type="hidden" value="10" name="weight" />
    <p><?=$item['Description'];?></p>
    <p><strong>Quantity:</strong>&nbsp;<input type="text" name="quantity" size="10" value="1"/></p>
    <p><strong>Price:</strong>&nbsp;$<?=number_format($item['Price'], 2);?></p>
    <p>
        <img onclick="document.getElementById('AddToCartForm'+<?=$index?>).submit();" style="cursor:pointer;" src="images/cart_button.png" width="100" height="111" alt="add to cart" />
    </p>
</form>

(如果我使用提交按钮,则数量会通过。)

3 个答案:

答案 0 :(得分:3)

您可以使用<input type="image">代替<img>。它就像提交按钮一样。

答案 1 :(得分:0)

你的onlcick只是提交,它没有做任何事情来改变quantity的价值......所以它正在做你告诉它的事情。在提交之前,您需要阅读quantity的值,将其加1,然后将新值重新写回quantity

答案 2 :(得分:0)

虽然已经给出了解决方案,但我认为我找到了问题的原因:

 document.getElementById('AddToCartForm'+<?=$index?>).submit();

我认为'onclick'提交了错误的表格。如果您将其更改为document.getElementById('AddToCartForm<?=$index?>').submit();

,它是否也会有效?