javascript - HTML javascript onclick 事件不适用于输入类型提交

标签 javascript php html

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

关于javascript - HTML javascript onclick 事件不适用于输入类型提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33086972/

相关文章:

javascript - 如何翻转 D3 可折叠径向树中的文本?

javascript - 防止 window.open 刷新父窗口

javascript - VueJS 2 : Catch event of direct child component

javascript - 如何合并批量查询的结果集?

php - Ajax 或 jQuery 页面重新加载,新数据添加到 sql 数据库

c# - asp.net分页改变高度

PHP "AT command"未在 nginx ubuntu 16.04 上执行

php - 仅使用 php 从 zip 文件中检索文件夹名称?

javascript - Jquery下拉菜单使用toggle和hover()问题

javascript - .draggable 不适用于 div