簡體   English   中英

HTML javascript onclick事件不適用於輸入類型提交

[英]HTML javascript onclick event not working on input type submit

 function myAlert(){ alert("yo"); } /* Recalculate cart */ function recalculateCart() { var subtotal = 0; /* Sum up row totals */ $('.product').each(function () { subtotal += parseFloat($(this).children('.product-line-price').text()); }); /* Calculate totals */ } /* Update quantity */ function updateQuantity(quantityInput) { /* Calculate line price */ var productRow = $(quantityInput).parent().parent(); var price = productRow.children('.product-price').text(); var quantity = $(quantityInput).val(); var linePrice = price * quantity; /* Update line price display and recalc cart totals */ productRow.children('.product-line-price').each(function () { $(this).fadeOut(fadeTime, function() { $(this).text(linePrice.toFixed(2)); recalculateCart(); $(this).fadeIn(fadeTime); }); }); } /* Remove item from cart */ function myFunction(removeButton) { /* Remove row from DOM and recalc cart total */ var productRow = $(removeButton).parent().parent(); productRow.slideUp(fadeTime, function() { productRow.remove(); recalculateCart(); }); return true; } 
 <?php session_start(); mysql_connect('localhost','root',''); mysql_select_db('ecommerce'); if(isset($_SESSION['username'])){ ?> <div class="shopping-cart"> <div class="column-labels"> <label class="product-image">Image</label> <label class="product-details">Product</label> <label class="product-price">Price</label> <label class="product-quantity">Quantity</label> <label class="product-removal">Remove</label> <label class="product-line-price">Total</label> </div> <?php $query = mysql_query("SELECT * FROM cart"); while($array=mysql_fetch_assoc($query)){ $dbtitle = $array['name']; $dbprice = $array['price']; $dbdescription = $array['description']; $dbproductid = $array['product_code']; echo $dbproductid; ?> <div class="product"> <div class="product-image"> </div> <div class="product-details"> <div class="product-title"><?php echo $dbtitle; ?></div> <p class="product-description"><?php echo $dbdescription; ?></p> </div> <div class="product-price"><?php echo $dbprice; ?></div> <div class="product-quantity"> <input id="quantity-option" type="number" value="2" min="1"> </div> <form action="cart.php" method="post"> <input type="submit" onclick="myFunction(this)" class="remove-product" name="delete" value="Remove"> <input type="hidden" name="remove_item" value="<?php echo $dbproductid; ?>"> </form> <div id="js-price" class="product-line-price"></div> </div> <?php } ?> 

當我按下提交時,什么也沒有發生,但是當我按下onclick alert()函數時,會出現警告框。 請幫幫我。 juz更新的帖子可幫助我檢查出什么錯誤。 您的幫助將不勝感激。

您沒有將removeButton參數傳遞給myFunction() 嘗試:

<input type="submit" onclick="myFunction(this)" class="remove-product" name="delete" value="Remove">

你為什么不使用jQuery的功能

$("#btn").click(function(e){
/* Remove item from cart */
e.preventDefault();
  /* Remove row from DOM and recalc cart total */
  var productRow = $("#btn").parent().parent();
  productRow.slideUp(fadeTime, function() {
    productRow.remove();
    recalculateCart();
  });
  return true;
});

您的HTML:-

<form action="cart.php" method="post">
      <input type="submit" id="btn" class="remove-product" name="delete" value="Remove">
        <input type="hidden" name="remove_item" value="<?php echo $dbproductid;  ?>">
    </form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM