簡體   English   中英

如何將數據從 Html 傳遞到 JavaScript?

[英]How do you pass data from Html to JavaScript?

我有一張Products表。 我從我的 controller 操作中獲得這些產品,我運行一個foreach循環並命名每個Product product 每個產品都有一個Id 然后,我在最后一個單元格旁使用刪除按鈕為該產品創建一行。

I can call with ajax (in JavaScript ) an Api to delete a Product from my database, the syntax of that Https request is like this: https://mywebsite.com/api/products/1 ( 1 is the Id and changes based我選擇刪除哪個產品)。

So the problem is that I call this Api after the user hits Yes on the confirmation dialog, so I need to do everything in the callback function of the dialog, and that is in the JavaScript , and I need to call the Https with the Id到最后,那么我如何獲得ID?

使用數據屬性,例如

 var article = document.getElementById('electriccars'); article.dataset.columns // "3" article.dataset.indexNumber // "12314" article.dataset.parent // "cars"
 <article id="electriccars" data-columns="3" data-index-number="12314" data-parent="cars">... </article>

https://developer.mozilla.org/ru/docs/Learn/HTML/Howto/Use_data_attributes

暫無
暫無

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

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