使用 jquery /mvc2 将孩子添加到父母列表中

发布于 2024-09-01 01:49:27 字数 289 浏览 1 评论 0原文

我的页面上有一个产品表,每个产品都有零种或多种颜色,颜色显示为产品下方的列表。在颜色之后,我有一个添加颜色的按钮。该按钮将使用父产品 id 对控制器进行 ajax 调用,该控制器将返回带有颜色信息的 JSON 对象。我的问题是在 DOM 中将产品 id 存储在哪里,我应该将其放在隐藏字段中并在“添加颜色”的单击事件中使用 jquery 来获取它吗?最好的方法是什么?

TIA,

John

编辑:页面最初是在服务器上呈现的,因此我不想使用 jquery 将 id 添加到页面。

I have a table of products on my page, each product has zero or more colors, the colors are shown as a list beneath the product. After the colors I have a button to add a color. The button will do an ajax call with the parent product id to a controller which will return a JSON object with color information. My problem is where to store the product id in the DOM, should I put it in a hidden field and use jquery in the click event of the "add color" to get to it? What is the best way to do this?

TIA,

John

EDIT: The page is initially rendered on the server so I don't want to use jquery to add the id's to the page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺孤伤于心 2024-09-08 01:49:27

快速但肮脏的方法是将其放入锚标记的 rel 属性中。或者您可以使用元数据插件: http://plugins.jquery.com/project/metadata。它能够从元素获取“data-something”属性。并且“data-something”属性对 HTML 5 有效(更多信息)。

所以你可以让你的 html 看起来像:

<tr data-productid="123"><td>...</td></tr>

在点击事件上:

var productId = $('tr-selector-here').metadata().productid;

The quick and dirty way would be to put it in an anchor tag's rel attribute. Or you can use the metadata plugin: http://plugins.jquery.com/project/metadata. It's capable of getting "data-something" attributes from an element. And the "data-something" attribute is valid for HTML 5 (more info).

So you can have your html look like:

<tr data-productid="123"><td>...</td></tr>

And on a click event:

var productId = $('tr-selector-here').metadata().productid;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文