Javascript JQuery,如何传递slug变量

Javascript JQuery,如何传递slug变量,javascript,python,jquery,django,Javascript,Python,Jquery,Django,我想知道如何将slug变量传递到JQuery/javascript中 我有一个包含项目的数据表,每行有两个按钮,一个(1)使用Django创建产品,另一个(2)应该使用JQuery/JS创建产品 要创建一个带有按钮1的产品,我发现它是直截了当的,而且解释得很好 我想使用JQuery/JS对按钮2执行相同的操作 按钮1 按钮2 在@31piy的帮助下,回复此SO帖子: 要选择具有数据组属性的所有元素,可以 在选择器[数据组]选择的元素上循环,并在 在迭代过程中,使用数据方法获取属性的值 请参见下面

我想知道如何将slug变量传递到JQuery/javascript中

我有一个包含项目的数据表,每行有两个按钮,一个(1)使用Django创建产品,另一个(2)应该使用JQuery/JS创建产品

要创建一个带有按钮1的产品,我发现它是直截了当的,而且解释得很好

我想使用JQuery/JS对按钮2执行相同的操作

按钮1 按钮2
在@31piy的帮助下,回复此SO帖子:

要选择具有数据组属性的所有元素,可以 在选择器[数据组]选择的元素上循环,并在 在迭代过程中,使用数据方法获取属性的值

请参见下面的示例:

$('[data-group]').each(function() {
  console.log($(this).data('group'));
})
我发现
常量url
可以表示为:

const url = $(this).data('url');
在控制台中登录时,我将获得所需的字符串

/api/products/7d49e267-f35d-4161-8eb5-0a815abbb083/create/
因此,完整的代码将成为:

$(function () {

    var createProduct = function() {
        const url = $(this).data('url');
       
        $.get(url)
        .done(function pollAsyncResults(data) {
          // bind pollAsyncResults to itself to avoid clashing with
          // the prior get request
          context: this
          // see the URL setup for where this url came from
          const pollAsyncUrl = `/api/products/poll_async_results/${data.task_id}`;
        })
    };

   $(".js-create-button").click(createProduct);

});
path('api/products/<uuid:slug>/create/', ProductsCreateView.as_view(), name="api-products-create"),
$(function () {

    var createProduct = function() {
        var slug = '{{ $slug }}';  /* some code/function that gets hold of the slug */
        const url = `/api/v1/products/${slug}/create/`;
       
        $.get(url)
        .done(function pollAsyncResults(data) {
          // bind pollAsyncResults to itself to avoid clashing with
          // the prior get request
          context: this
          // see the URL setup for where this url came from
          const pollAsyncUrl = `/api/products/poll_async_results/${data.task_id}`;
        })
    };

   $(".js-create-button").click(createProduct);

});
$('[data-group]').each(function() {
  console.log($(this).data('group'));
})
const url = $(this).data('url');
/api/products/7d49e267-f35d-4161-8eb5-0a815abbb083/create/
$(function () {

    var createProduct = function() {
        const url = $(this).data('url');
       
        $.get(url)
        .done(function pollAsyncResults(data) {
          // bind pollAsyncResults to itself to avoid clashing with
          // the prior get request
          context: this
          // see the URL setup for where this url came from
          const pollAsyncUrl = `/api/products/poll_async_results/${data.task_id}`;
        })
    };

   $(".js-create-button").click(createProduct);

});