2012-01-27 147 views
0

我已使用以下代码在我的购物车网站上显示产品详细信息。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>&nbsp;</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">&nbsp;</div> 

    </form> 
</div> 

在我的页面中有一个数量字段,并加入购物车按钮。如果买方点击添加到购物车按钮而没有输入数量字段,则会弹出错误消息。为此,我使用了下面的JavaScript代码。

function form_quantity(val){ 


var enteredqty = document.getElementById('quantity_'+val).value; 
if(enteredqty =='') 
{ 
alert(Please enter quantity); 
} 


} 

但它不起作用。我无法追踪错误。我如何纠正我的代码?

+0

你可以做'if(!enteredqty)',虽然这可能不是你的问题的答案。添加'alert(enteredqty)'以确保该值正在被拾取。还请检查您的控制台是否存在任何引发的JavaScript错误。 – xbonez 2012-01-27 06:33:02

回答

1

你已经有了一个字符串字面量没有引号:

alert(Please enter quantity); 

你需要说:

alert("Please enter quantity"); 
// OR 
alert('Please enter quantity'); 

(当你说你无法跟踪你的错误,究竟做了你试试吧?如果使用Chrome,它有内置的调试工具,或者Firefox可以下载Firebug,并且这些工具可以很容易地告诉你这样的错误。)

为了抢占你的下一个问题,一次你修正了上面的错误,你会发现虽然警报显示表单仍然提交。您需要更新您的onclick来回报您的form_quantity()函数的结果,并返回false当你不想提交先走(即,当有一个验证错误):

<form name="product_form" id="product_form" method="post" 
     onsubmit="return form_quantity(<?php echo $productid; ?>);"></form> 

<script> 
function form_quantity(val){ 
    var enteredqty = document.getElementById('quantity_'+val).value; 
    if(enteredqty === '') { 
     alert('Please enter quantity'); 
     return false; 
    } 
} 
</script> 

+0

谢谢。它正在工作。 – designersvsoft 2012-01-27 06:42:58

+0

谢谢。你也解决了我的下一个问题。非常感谢。 – designersvsoft 2012-01-27 06:55:30

1
function form_quantity(val){ 
    var enteredqty = document.getElementById('quantity_'+val).value; 
    alert(enteredqty);// check the givel value is right. 
    if(enteredqty ==''){ 
     alert("Please enter quantity");// double qute added. 
    } 
} 

选中此项,您可能会发现一些路径。

+0

谢谢。它正在工作。 – designersvsoft 2012-01-27 06:43:49