使用Javascript或Jquery向url传递参数

2024-05-19 02:25:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有Django代码

视图.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

^{pr2}$

我有一个复选框的唯一uuid。通过使用它,我可以使用Django获取与UUID相关的项视图。依据这个我的url将是https://localhost:8000/page1?ids=asdf-a972j-aswer&ids=asdf6-asdfewq-asdfwq-dfasfd&ids=asdf0-asdfasdf-asdf

但我需要这样的网址https://localhost:8000/page1?ids=sdf-asdf23-as2q3r,sdfqwe-232sasdf-23rwdefr,wqerqr-3qwq2r-23rq23r

如何使用javascript实现这一点?在

感谢你的回答


Tags: djangohttps视图idslocalhostuuidrequesthtml
1条回答
网友
1楼 · 发布于 2024-05-19 02:25:19

我试过了,成功了。在

工作代码:

$(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的url

^{pr2}$

相关问题 更多 >

    热门问题