如果选中复选框,请添加 URL

Add in the URL if a check box is checked

我在页面上有一个带有复选框的下拉表单,一旦用户 select 有过滤器,我需要添加 select 在 [=27= 中编辑的那个复选框的值] 以便在 CMS 中完美搜索

例如好的 URL 看起来像那样。

https://www.zyris.io/sort-and-filter?interests-2=Animals%7CArt&ages=Teens%20(13%20to%2017)&days-2=Today

我写了它的代码。但它也将未 selected 的变量添加为 null,因此无法使用 URL.

进行搜索

它的任何解决方案,这样我就可以只添加那些 selected 的变量。

这是我的 JavaScript 代码:

document.getElementById("search").onclick = function formJS() {
    var a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, url = null;
    if ($('#Animal').is(":checked")) {
        a = "Animals";
        console.log("Animals is checked");
    }


    if ($('#Ar').is(":checked")) {
        b = "%7CArt";
        console.log("Art is checked");
    }


    if ($('#Histor').is(":checked")) {
        c = "%7CHistory";
        console.log("History is checked");
    }


    if ($('#Scienc').is(":checked")) {
        d = "%7CScience";
        console.log("Science is checked");
    }


    if ($('#Technolog').is(":checked")) {
        e = "%7CTechnology";
        console.log("Technolog is checked");
    }


    if ($('#Today2').is(":checked")) {
        f = "Today";
        console.log("Today is checked");
    }


    if ($('#Next7Days2').is(":checked")) {
        g = "%7CNext%207%20Days";
        console.log("Next 7 Days is checked");
    }


    if ($('#ThisMonth2').is(":checked")) {
        h = "%7CThis%20Month";
        console.log("This Month is checked");
    }


    if ($('#NextMonth2').is(":checked")) {
        i = "%7CNext%20Month";
        console.log("Next Month is checked");
    }


    if ($('#Morning').is(":checked")) {
        j = "%7CMorning";
        console.log("Morning is checked");
    }


    if ($('#Afternoon').is(":checked")) {
        k = "%7CAfternoon";
        console.log("Afternoon is checked");
    }

    if ($('#Evening').is(":checked")) {
        l = "%7CEvening";
        console.log("Evening is checked");
    }


    if ($('#firstPrice').is(":checked")) {
        m = "%240%20—%20%2420";
        console.log("0 — 20 is checked");
    }


    if ($('#secondPrice').is(":checked")) {
        n = "%7C%2420%20—%20%2450";
        console.log(" —  is checked");
    }


    if ($('#thirdPrice').is(":checked")) {
        o = "%7C%2450%2B";
        console.log("+ is checked");
    }


    if ($('#Kids').is(":checked")) {
        p = "%7CKids%20(Up%20to%207)";
        console.log("Kids (Up to 7) is checked");
    }


    if ($('#Tweens').is(":checked")) {
        q = "%7CTweens%20(8%20to%2012)";
        console.log("Tweens (8 to 12) is checked");
    }


    if ($('#Teens').is(":checked")) {
        r = "%7CTeens%20(13%20to%2017)";
        console.log("Teens (13 to 17) is checked");
    }



    if ($('#Adults').is(":checked")) {
        s = "Adults%20(18%2B)";
        console.log("Adults (18+) is checked");
    }


    url = 'https://www.zyris.io/sort-and-filter?ages='
    e + f + g + h '&interests-2=' + a + b + c + d '&prices-2='
    i + j + k + l '&times= &days-2='
    m + n + o + p '                                       
    setTimeout(function() {
        window.location.href = url;
    }, 2000);



}

使用数组代替大量不同的变量。然后就可以在创建URL.

的时候加入数组了
let interests = [];
if ($('#Animal').is(":checked")) {
    interests.push("Animals");
    console.log("Animals is checked");
}
if ($('#Ar').is(":checked")) {
    interests.push("Art");
    console.log("Art is checked");
}
// and so on
let times = [];
if ($('#Today2').is(":checked")) {
    times.push("Today");
    console.log("Today is checked");
}
// and so on, similarly for prices and ages

let interests_param = encodeURIComponent(interests.join('|'));
let times_param = encodeURIComponent(times.join('|'));
let prices_param = encodeURIComponent(prices.join('|'));
let ages_param = encodeURIComponent(ages.join('|'));

let url = `https://www.zyris.io/sort-and-filter?ages=${ages_param}&prices-2=${prices_param}&times=${times_param}&interests-2=${interests_param}`;

做了几个样本,你必须填写其余的。此代码适用于任意数量的复选框。除非添加新的“类别”,否则不必更改代码。

$(document).ready(()=> {
  $("#search").click(()=> {
    const interests = $(".I:checked");
    let I = interests.length>0 ? "interests=" : "";
    for (let i=0; i<interests.length; i++) {
        I += interests[i].getAttribute("V") + "|";
      };
    I = I.slice(0, -1); // to prevent trailing |
    
    const ages = $(".A:checked");
    let A = ages.length>0 ? "ages=" : "";
    for (let i=0; i<ages.length; i++) {
        A += ages[i].getAttribute("V") + "|";
      };
    A = A.slice(0, -1);
    
    // Repeat above for categories P & T for prices and times
    
    let url = "https://ww.zyris.io/sort-and-filter?";
    let path = I;
    path += path ? A ? "&" + A : "" : A; // to prevent leading/trailing &'s
    $("#raw").text(path);
    $("#url").text(encodeURI(url + path));
  })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>

<p>Interests:
  <input type="checkbox" id="Animal" V="Animals" class="I" />
  <label for="Animal">Animals</label>
  &nbsp;
  <input type="checkbox" id="Art" V="Art" class="I" />
  <label for="Art">Art</label>
  &nbsp;
  <input type="checkbox" id="History" V="History" class="I" />
  <label for="History">History</label>
</p>

<p>Ages: 
  <input type="checkbox" id="Today" V="Today" class="A" />
  <label for="Today">Today</label>
  &nbsp;
  <input type="checkbox" id="Next7Days" V="Next 7 Days" class="A" />
  <label for="Next7Days">Next 7 Days</label>
  &nbsp;
  <input type="checkbox" id="ThisMonth" V="This Month" class="A" />
  <label for="ThisMonth">This Month</label>
</p>
<p>
  <button id="search">Search</button><br/>
  Raw path: <span id="raw"></span><br/>
  Url: <span id="url"></span>
</p>

我使用了任意属性 V 来保存要包含在查询字符串中的值。

需要进行一些边界检查以防止出现 https://x.com?&ages=... 或尾随 | 等情况,即使它们在大多数情况下都有效。