普通网友 2018-01-09 11:35
浏览 164

Ajax更改URL参数

Now i have javascript code that switch page(reloads) it to something like this after user checks a checkbox

http://localhost:8000/products/all-products?country=1

when user clicks on other checkbox it changes 1 to 2, for example. But what i would like to achieve is something like this : ?country=[1,2] or if there is only one parameter than it can be ?country=1 or ?country=[1], if there are checked more than one checkbox, it needs to be separated by a comma.

JS code :

   $('input[type="checkbox"]').on('change', function(e){
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    var data = {},
        fdata = [],
        loc = $('<a>', {href:window.location})[0];

    $('input[type="checkbox"]').each(function(i){
        if(this.checked){
            if(!data.hasOwnProperty(this.name)){
                data[this.name] = [];
            }
            data[this.name].push(this.value);
        }
    });

    $.each(data, function(k, v){
        fdata[k] = [v.join(',')];

    });

    var countryArr = fdata["country"];
    fdata = countryArr.join('&');
    $.post('/wines/all-wines/', fdata);
    console.log(fdata);

    if(history.pushState){
        history.pushState(null, null, loc.pathname+'?country='+fdata);

        window.location.href = loc.pathname+'?country='+fdata;
    }
});

Thank you very much!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何让企业微信机器人实现消息汇总整合
    • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
    • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
    • ¥15 TLE9879QXA40 电机驱动
    • ¥20 对于工程问题的非线性数学模型进行线性化
    • ¥15 Mirare PLUS 进行密钥认证?(详解)
    • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
    • ¥20 想用ollama做一个自己的AI数据库
    • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
    • ¥15 请问怎么才能复现这样的图呀