다음 코드를 사용하여 장바구니 웹 사이트에 제품 세부 사항을 표시했습니다.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>
내 페이지의 수량 필드 및 장바구니 버튼이 있습니다. 구매자가 수량 필드를 입력하지 않고 장바구니에 추가 버튼을 클릭하면 오류가 팝업됩니다. 그 때문에 나는 다음과 같은 자바 스크립트 코드를 사용했다.
function form_quantity(val){
var enteredqty = document.getElementById('quantity_'+val).value;
if(enteredqty =='')
{
alert(Please enter quantity);
}
}
하지만 작동하지 않습니다. 나는 그 오류를 추적 할 수 없었다. 코드를 수정하려면 어떻게해야합니까?
당신은 따옴표로 문자열 리터럴있어
'if (! entriesqty)'를 할 수는 있지만 질문에 대한 답변이 아닐 수도 있습니다. 값을 확인하려면'alert (enteredqty)'를 추가하십시오. 또한 콘솔에 javascript 오류가 있는지 확인하십시오. – xbonez