使用Javascript或Jquery将参数传递给url

亚什万斯

我有Django代码

views.py

def compare(request):
    import ipdb;ipdb.set_trace()
    ids = request.GET.getlist('ids', '')
    products = []
    product_matrix = []
    for uuid in ids:
        try:
            product = LoanProduct.objects.get(uuid=uuid)
            products.append(product)
        except LoanProduct.DoesNotExist:
            pass
    if products:
        product_matrix = make_product_matrix(products)


    print product_matrix
    return TemplateResponse(request, "products/compare.html", {'product_matrix': product_matrix})

page1.html

<div class="row">
    <div class="col-md-4 col-lg-3 col-sm-6">
      <form action="" method="post">{% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="btn btn-primary">Search</button>
      </form>
    </div>
    <form action="/products/compare/" method="get">
    <div class="col-md-8 col-lg-9 col-sm-6">
      {% if products %}
      <table class="table table-hover">
          <thead>
              <tr>
                  <th> Check to Compare </th>
                  <th> Product Name</th>
                  <th> Quantum of finance </th>
                  <th> Interest Rate </th>
                  <th> Nature of Security</th>
                  <th> Margin </th>
              </tr>
          </thead>
          <tbody>
            {% for product in products %}
            <tr data-uuid="{{ product.uuid }}" id="uuid">
                  <th><input type="checkbox" name="ids" id="checkbox" value= {{ product.uuid }} /></th>
                  <td>{{ product.name }}</td>
                  <td>{{ product.get_finance_quantum }}</td>
                  <td>{{ product.get_interest_rate }}</td>
                  <td>{{ product.security }}</td>
                  <td>{{ product.get_margin }}</td>
              </tr>
            {% endfor %}
          </tbody>
      </table>
      {% endif %}
    </div>
    <button type="submit" id="compare" class="btn pull-right btn-success"> Compare </button>
</form>
</div>

我有一个复选框唯一的uuid。通过使用Django视图,我得到了与该UUID相关的项目。https://localhost:8000/page1?ids=asdf-a972j-aswer&ids=asdf6-asdfewq-asdfwq-dfasfd&ids=asdf0-asdfasdf-asdf

但是我需要这种方式的URL https://localhost:8000/page1?ids=sdf-asdf23-as2q3r,sdfqwe-232sasdf-23rwdefr,wqerqr-3qwq2r-23rq23r

如何使用javascript执行此操作?

赞赏的答案

亚什万斯

我尝试过并使其正常工作。

工作代码:

$(document).ready(function() {
    $('#compare').click(function() {
        var uuids = '';
        var length = $("input[type='checkbox']:checked").length;
        console.log(length);
        $("input[type='checkbox']:checked").each(function(index){
            uuids = uuids + $(this).val();
            if (index < length-1) {
                uuids = uuids + ',';
            }
        });
        url = '/products/compare/?ids=' + uuids;
        window.location.replace(url);
    });
});

最后,这给了我以uuid与逗号分隔的网址

https://localhost:8000/page1?ids=sdf-asdf23-as2q3r,sdfqwe-232sasdf-23rwdefr,wqerqr-3qwq2r-23rq23r

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用JQuery POST将变量传递给URL参数

来自分类Dev

将参数传递给JavaScript中的URL

来自分类Dev

将参数传递给JavaScript中的url回调函数

来自分类Dev

将整数作为参数php / javascript传递给url

来自分类Dev

如何将参数传递给javascript onclick函数。请不要使用jquery

来自分类Dev

在jquery中使用on()时将参数传递给函数

来自分类Dev

使用类时将参数传递给jquery函数

来自分类Dev

使用Jquery将参数传递给Rails Helper

来自分类Dev

在jquery中使用on()时将参数传递给函数

来自分类Dev

如何使用 Jquery Ajax 将参数传递给 mvc ActionResult?

来自分类Dev

将查询参数传递给使用JavaScript嵌入的iframe

来自分类Dev

html使用javascript将参数传递给框架

来自分类Dev

在JavaScript中使用()()将参数传递给匿名函数

来自分类Dev

如何使用 Jquery 将选择值传递给 url

来自分类Dev

将url参数传递给ListView queryset

来自分类Dev

将参数传递给新URL

来自分类Dev

将参数传递给BLOB对象URL

来自分类Dev

Symfony:将参数传递给Ajax URL

来自分类Dev

将参数传递给Django URL失败

来自分类Dev

将参数传递给URL对象

来自分类Dev

烧瓶-将参数传递给传出的url。

来自分类Dev

将参数传递给laravel的URL :: to()方法

来自分类Dev

将URL参数传递给Python

来自分类Dev

Symfony:将参数传递给Ajax URL

来自分类Dev

将URL参数传递给输入文本

来自分类Dev

jQuery将参数传递给插件

来自分类Dev

将参数传递给jQuery函数动画

来自分类Dev

jQuery:将参数传递给模态

来自分类Dev

将参数传递给Jquery Fadeout函数