html 如何确保它重定向到某些网址只有当按钮被点击javascript

hgtggwj0  于 2023-05-05  发布在  Java
关注(0)|答案(2)|浏览(180)

所以我以某种方式建立了它转移总从结帐页到索引页,这是在这种情况下付款页,但这是一个问题;它自动转到索引页时,在结帐页面,但我希望它这样做,只有当按钮被点击。我试图做一些与addEventListener,但它只是去index.html?而不是像index.html这样的东西?总计=320.00
验证码:

<div class="total">
    <form action="index.html" class="total">
    <h3 style="text-align:center; display: none;">Subtotal: <br><span id="subtotal"></span></h3>
    <h3 style="text-align:center;">Total: <br><span id="total">$0</span></h3>
    <a><button type="submit" class="buy-btn" style="background-color: rgb(29, 4, 216);">Continue to purchase</button></a>
    <a href="index.html" onclick="alert('You will be redirected to the home page')"><button style="background-color: rgb(216, 4, 4);">Cancel</button></a>
  </div>
</form>
  <!--It does clear but wonder , make that you cant click on button if that function is done-->
</body>
<script>
  const cartItemsDiv = document.querySelector(".cart-items");
  const storedCartItem = localStorage.getItem("selectedItems");  
  let subtotal = 0;

  const continueButton = document.getElementById("buy-btn");
  const clearButton = document.getElementById("clear-button");

function clearCartItems() {
  // Clear the HTML content of the cart-items div
  const cartItemsDiv = document.querySelector(".cart-items");
  cartItemsDiv.innerHTML = "";

  // Set the subtotal and total to $0
  const subtotalSpan = document.getElementById("subtotal");
  const totalSpan = document.getElementById("total");
  subtotalSpan.innerHTML = "";
  totalSpan.innerHTML = "$0";

  // Remove the selected items from localStorage
  localStorage.removeItem("selectedItems");

  alert("Added items will be cleared.");
  // Change the URL to the index page and prevent going to the checkout page
  window.location.href = "sproduct6.html";
  history.replaceState(null, "", window.location.href);

  // Update the UI
  document.getElementById("added-products").innerHTML = "You have no items in your cart!";
  document.getElementById("buy-btn").style.display = "none";
  document.getElementById("clear-button").style.display = "none";

   if (cartItemCount == 0) {
    setTimeout(function() {
      alert("You have no items in your cart. Let's get back to product page.");
      window.location.href = ('sproduct6.html');
    }, 50); // 3 second delay 
    document.getElementById("added-products").innerHTML = "You have no items in your cart!";
    continueButton.style.display = "none";
    clearButton.style.display = "none";
    // Save the visibility state of the buttons
    localStorage.setItem("buttonsVisible", "false");
  } else {
    document.getElementById("added-products").innerHTML = "YOU ADDED THIS PRODUCTS";
  } 
}

if (storedCartItem) {
  const cartItem = JSON.parse(storedCartItem); // Parse the storedCartItem string into an array

  for (let i = 0; i < cartItem.length; i++) {
    const cartItemElement = document.createElement("div");  

    
    cartItemElement.innerHTML = `
        <hr />
        <h3>${cartItem[i].title}</h3>  
        <p>Price: ${cartItem[i].price}</p>
        <p>License: ${cartItem[i].license}</p>
        <img src="${cartItem[i].image}" width='30%'/><br>
        <audio controls style="width:50%;">
          <source src="${cartItem[i].audio}" type="audio/mp3"> 
        </audio>
        `;
    // ADD DELETE OPTION 

    cartItemsDiv.appendChild(cartItemElement);

    const price = Number(cartItem[i].price.replace("$", "")); // Parse price as a number and remove dollar sign
    subtotal += price;
    // MAKE REPLACE TITLE TO YOU HAVE NO ITEMS IN YOUR CART
  }

  const subtotalElement = document.querySelector("#subtotal");
  subtotalElement.textContent = `$${subtotal.toFixed(2)}`;

  const totalElement = document.querySelector("#total");
  const total = subtotal;
  totalElement.textContent = `$${total.toFixed(2)}`;

  // Add this line to redirect to index.html and pass the total as a query parameter
  window.location.href = 'index.html?total=' + total.toFixed(2);

  totalElement.textContent = `$${total.toFixed(2)}`;
  const totalPriceInput = document.querySelector("#total");
  totalPriceInput.value = total.toFixed(2);

  passtotal(); // Call the function to pass the total price to the index page

function passtotal(){
  var total = document.getElementById('total').value;
  var subtotal = document.getElementById('subtotal').value;
  localStorage.getItem("total", total);
  return false;
}
}
</script>

这里是索引代码

<div class="sr-root">
      <span id="total"></span>
      <div class="sr-main">
        <h1>Pay with a bunch of payment option</h1>

        <form id="payment-form" method="POST">
          <input type="text" name="email" id="email" placeholder="E-Mail" required>
          <input type="text" name="name" id="name" placeholder="Full Name" required>
    
          <div id="payment-element"></div>
    
          <button>Pay</button><!--Make functional-->
          <!--Make total price hidden and that total of this is same as in checkout and that its connected to ruby-->
          <div id="error-messages"></div>
        </form>        
      </div>
    </div>
    <script>
      // get the value of the "total" parameter from the URL
      const urlParams = new URLSearchParams(window.location.search);
      const total = urlParams.get("total");

      // set the text content of the span element to the total value
      const totalSpan = document.getElementById("total");
      totalSpan.textContent = total;
    </script>
zte4gxcn

zte4gxcn1#

根据您的代码,有几件事情需要研究

1错误的标记层次结构

<div class="total">
    <form ...>
    <h3 ...>
    <h3 ...>
    <a><button  ...></a>
    <a ...><button ...></a>
  </div>
</form>

你在<div>里面有<form>,但是你关闭了外面的标签...它应该是,例如

<div class="total">
    <form ...>
      <h3 ...>
      <h3 ...>
      <a><button  ...></a>
      <a ...><button ...></a>
    </form>
  </div>

2号锚钉环绕钛板
你有

<a>
  <button type="submit" ...>Continue to purchase</button>
</a>

你到底想干什么<a>已经有了一个行为,你在它里面有一个按钮,它的类型是submit,所以你要分配另一个行为,试着只有button,而不用anchor包围它,这样你就可以期待正常的按钮行为

3如何工作?

你有一个类型为submit的按钮,它将提交围绕这个标签的form,在这种情况下,你的<form action="index.html" ...>将被调用并提交,它会这样做,它会将你发送到index.html页面,因为这就是你所要求的,而在GET方法(默认)中,这意味着它将输入到URL,而不是像POST那样将其传递到内存中...但是你在form里面没有任何输入,所以它不传递任何东西,如果你添加一个输入,它会发生你期望的值,比如

<div class="total">
    <form action="index.html">
      <h3 ...>
      <h3 ...>
      <button  ...>
      <a ...>
      <input type="hidden" name="total" value="200" />
    </form>
  </div>

它会将您发送到index.html?total=200

4您的索引表单

你的按钮,放置得很好,没有任何锚标签,但它是一个简单的按钮,具有默认行为,因为你只是将它作为<button>Pay</button>这将作为一个按钮样式,并将作为一个提交
你也有它在一个表单与POST方法,这意味着你可能希望它发送数据在formPOST的方式,我们通常做两件事之一(虽然有更多的方式来完成相同的)
1.与您在结帐表单中所做的相同,我们添加type="submit",这将触发具有此按钮的form
1.我们添加一个javascript函数或代码,它将在单击时运行,如<button type="button" onclick="payNow()">Pay</button>,在javascript中,它将运行名为payNow的函数,您应该在script代码中编写该函数
我们必须通过指定type="button"来告诉button,我们希望将其视为“哑”按钮,否则您将使用默认行为,这往往是每个浏览器的实现(web或移动的)已经实现,也许它会充当type="submit"或者它会充当你不知道的type="button",这就是为什么我们倾向于通过在按钮标签上使用te type属性来设置期望值的原因
我希望这4个技巧可以帮助你“摆脱困境”,继续你的代码,保持下去,不要给予,下个月你会编码,甚至不用想它😊

u0sqgete

u0sqgete2#

代码太多了。将与你的问题相关的行分开。在任何情况下,都不要在锚元素中嵌套按钮。锚元素的href应该通过编程方式更新,以包含您想要传递的查询参数,例如:

<a href="https://google.com?dollars=12&cents=05">go</a>

您可以将金额存储在localStorage中,但前提是您正在打开相同来源/域名的页面。
使用URL constructormyurl.searchParams.append("dollar",12)
参见https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams

相关问题