简体   繁体   English

我如何从一个页面制作一个按钮,以自动触发另一个页面的特定按钮?

[英]How i can make a button ,from a page , to automatically trigger a specific button from another page?

I am building a project for school and I encounter a problem.我正在为学校建立一个项目,但遇到了一个问题。 I made an ecommerce website,and in the store page I created the cart on the bottom.我做了一个电子商务网站,在商店页面的底部创建了购物车 now when I click the the product button it's added in the cart.现在,当我单击产品按钮时,它已添加到购物车中。 Now i created a page for each product with description and I have an Add to cart button there too, how can I click on that button and that automatically go on store page and click the specific button for the product?现在我为每个产品创建了一个带有描述的页面,我也有一个“添加到购物车”按钮,我如何单击该按钮并在商店页面上自动 go 并单击产品的特定按钮?

here is my store code:这是我的商店代码:

<img class="shop-item-image" src="imaginiproduse/p1.jpg">
<span class="shop-item-title">Busuioc</span>
<div class="shop-item-details">
      <span class="shop-item-price">$12.99</span>
      <button class="btn btn-primary shop-item-button " type="button" >ADD TO CART</button> <-- HERE IS THE BUTTON 
</div>
</div>

Here is my individual product page code:这是我的个人产品页面代码:

<div class="container">
    <img src="../imaginiproduse/p1.jpg" style="max-width: 250px;" >
    <div class="text">
        <h2>Busuioc</h2>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsum nesciunt explicabo omnis dolores, sunt odio ducimus consequuntur distinctio quo et nemo molestiae saepe nostrum a iste! Modi amet ab dolore.</p>               
    </div>
    <p><button  type="button">ADD TO CART</button></p> <--- HERE IS MY OTHER BUTTON     
</div>

How are you adding elements to the cart and storing items in the cart?您如何将元素添加到购物车并将商品存储在购物车中?

if you are using vanilla javaScript and storing cart items in an array, you can not do that.如果您使用香草 javaScript 并将购物车项目存储在数组中,则不能这样做。

You have to store cart items in a way that is accessible through pages.您必须以可通过页面访问的方式存储购物车项目。 For example:例如:

  • you can use cookies (server side)您可以使用 cookies (服务器端)
  • you can use HTML5 local file storage (client side. temporary)您可以使用 HTML5 本地文件存储(客户端。临时)
  • use some sate management package like redux (may need nodejs)使用一些状态管理 package 像 redux (可能需要 nodejs)

As stated you have options and typically this would be done with cookies (secure cookies if its sensitive stuff )sidenote too, cookie are not always server-side and can be set on the client and read by the client OR server so it works both ways, localstorage (only read by the browser, but is persistent) or server-side sessions, server cookies (would render on the server and generate links etc)如前所述,您有选项,通常这将使用 cookies 完成(如果其敏感内容,请确保 cookies )旁注,cookie 并不总是服务器端,可以在客户端上设置并由客户端或服务器读取,因此它可以双向工作,本地存储(仅由浏览器读取,但持久)或服务器端会话,服务器 cookies(将在服务器上呈现并生成链接等)

Now with that said it seems more like you are doing this via the client side and using localStorage at the moment.话虽如此,您现在似乎更像是通过客户端执行此操作并使用 localStorage。

How you have your question worded is a bit confusing, but if I understand correctly you basically want to store the 'add to cart' with the item, so they can view the card, see their item and then also click back to their item from the cart etc.你的问题措辞有点令人困惑,但如果我理解正确,你基本上想将“添加到购物车”与该项目一起存储,这样他们就可以查看卡片,查看他们的项目,然后点击返回到他们的项目购物车等

The flow of that, using localStorage could be使用 localStorage 的流程可能是

  1. User views the item detail page用户查看商品详情页面
  2. User clicks add to cart --> This triggers a function to append the itemID to the cart localstorage ( localStorage can only be a string, so you would end up stringifying your object before storing it ) possibly somthing like用户点击添加到购物车 --> 这会触发 function 到 append 的 itemID 到购物车 localstorage ( localStorage 只能是一个字符串,所以你最终会在存储之前将你的 ZA8CFDE6331BD4thingB62AC96F8911 之类的 ZA8CFDE6331BD4thingB666Z 字符串化)

 //click action (jquery syntax) $('.addToCart').on("click",()=>{ //add to cart clicked //Grab localStorage first let data = localStorage.getItem('cart'); //check if there is anything in it, otherwise don't stringigy if ( data.== undefined && data;== "") { //Means there is an actual localStorage object called cart AND its not empty //we can then proceed with stringifying it try{ //turn the localStorage into an object data = JSON.parse(data); //now that you have the object you will push the item ID to the array data,push(itemID). }catch(e){ //yikes, its not valid JSON console.error(e) } } })

I have not tested the code but it gives you an idea of the flow, then removing an item can just be a simple pop of the item.我没有测试过代码,但它让您了解流程,然后删除一个项目可以只是项目的简单弹出。

When viewing it, you just pull the localStorage item, convert to an array and loop over it.查看它时,您只需拉出 localStorage 项目,转换为数组并循环它。 You can also store more items in the object too, things like name, id, price etc OR you can run the idea against an API call etc.您还可以在 object 中存储更多项目,例如名称、ID、价格等,或者您可以针对 API 调用等运行该想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个页面更改按钮的状态? - How can I change the state of a button from another page? PHP:如何从另一个页面的按钮触发javascript函数 - PHP : how to trigger javascript function from a button in another page 如何从 querySelecotrAll/forEach 触发一个特定按钮? - How can I trigger one specific button from querySelecotrAll/forEach? 如何从第一页的按钮重新加载第二页? - How can I reload page two from button in page one? 触发button.click事件来自另一页 - Trigger the button.click event From another page react native:当我从另一个页面单击按钮时,如何更改视图? - react native: How can I change a view when i click a button from another page? 如何让按钮将我的页面重定向到另一个页面(取决于输入) - How can I make a button redirect my page to another page (depending on the input) 如何使用addEventListener使按钮将页面重定向到另一个页面? - How can I make a button redirect my page to another page using addEventListener? 如何在页面加载时自动点击Chrome扩展程序? - How can I make a Chrome extension automatically click a button when a page loads? 如何在页面加载时停止javascript中的警报触发,并在单击按钮时使其运行? - How can I stop an alert in javascript from firing when the page loads, and make it run when a button is clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM