我已使用以下代码在我的购物车网站上显示产品详细信息。javascript空字段验证
<div id="inner_right">
<form name="product_form" id="product_form" method="post" onsubmit="form_quantity(<?php echo $productid; ?>);">
<input type="hidden" name="hidden_<?php echo $productid; ?>" id="hidden_<?php echo $productid; ?>" />
<h1>Product Details of <?php echo $fetchproductname; ?></h1>
<div> </div>
<div id="product_left"><img src="<?php echo $path.$fetchimage; ?>" alt="" width="400" height="300" /></div>
<div id="product_right">
<div><strong>Category Name:</strong> <?php echo $categoryname; ?></div>
<p><strong>Product Number:</strong> <?php echo $fetchproductno; ?></p>
<p><strong>Price:</strong> <span class="price">$<?php echo $fetchproductprice; ?></span></p>
<p><strong>Stock:</strong> <?php echo $fetchproductstock; ?> nos</p>
<?php
$select_quantity = "SELECT * FROM `tbl_cart` WHERE `intProductid` = '".$productid."' AND `intSessionid` = '".$globalsessionid."'";
$select_quantity_res = mysql_query($select_quantity);
$sel_qty_num = mysql_num_rows($select_quantity_res);
$fetch_quantity = mysql_fetch_array($select_quantity_res);
$fetch_proid = $fetch_quantity['intProductid'];
$fetch_exqty = $fetch_quantity['intQuantity'];
?>
<p><strong>Quantity:</strong> <input name="quantity_<?php echo $productid; ?>" id="quantity_<?php echo $productid; ?>" value="<?php echo $fetch_exqty; ?>" class="quantity" type="text" /></p>
<div class="submit">
<button id="registerButton" type="submit">Add To Cart</button>
</div>
<input type="hidden" name="cart" id="cart" value="<?php echo $productid; ?>" />
</div>
<div class="clear"> </div>
</form>
</div>
在我的页面中有一个数量字段,并加入购物车按钮。如果买方点击添加到购物车按钮而没有输入数量字段,则会弹出错误消息。为此,我使用了下面的JavaScript代码。
function form_quantity(val){
var enteredqty = document.getElementById('quantity_'+val).value;
if(enteredqty =='')
{
alert(Please enter quantity);
}
}
但它不起作用。我无法追踪错误。我如何纠正我的代码?
你可以做'if(!enteredqty)',虽然这可能不是你的问题的答案。添加'alert(enteredqty)'以确保该值正在被拾取。还请检查您的控制台是否存在任何引发的JavaScript错误。 – xbonez 2012-01-27 06:33:02