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;
});

您的网页 :-

<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>