简体   繁体   English

Select2 onchange选择选项,然后根据选项附加或删除网址

[英]Select2 onchange select option then append or strip url depending on option

Currently I have a select dropdown filter setup via php like this: 目前,我有一个通过php的select下拉过滤器设置,如下所示:

<?php if($this->config->get("config_show_tmall_only")){?>
  <?php if(isset($code) && $code=="tmall"){?>
  <select id="tmallFilter">
    <option value="&code=tmall">Tmall only</option>
    <option value="">Taobao &amp; Tmall</option>
  </select>
  <?php }else{?>
  <select id="tmallFilter">
    <option value="">Taobao &amp; Tmall</option>
    <option value="&code=tmall">Tmall only</option>
  </select>
  <?php }?>
<?php }?>

It detects if &code=tmall is in the url and runs the first block 它检测&code=tmall是否在url中并运行第一个块

I am appending &code=tmall through this script that runs exactly how I need 我正在通过完全按照我的需要运行的脚本添加&code=tmall

$('#tmallFilter').bind('change', function () {
    var brwsr_url=document.URL;
    var redirecturl= brwsr_url + $(this).val();
    location.href = redirecturl;
});

The problem I am running into is when i need to go back to Taobao & Tmall I can only add to the url, not strip characters based on that select. 我遇到的问题是,当我需要返回Taobao & Tmall我只能添加到url,而不能基于该选择删除字符。

Is there any way I can strip &code=tmall from the url when I select the Taobao &amp; Tmall 当我选择Taobao &amp; Tmall时,有什么方法可以从网址中删除&code=tmall Taobao &amp; Tmall Taobao &amp; Tmall option? Taobao &amp; Tmall选项?

This is to strip away &code=tmall specifically using String.replace : 这是专门使用String.replace剥离&code=tmall String.replace

$('#tmallFilter').bind('change', function () {
    var brwsr_url = document.URL;
    var redirecturl= brwsr_url.replace(/&code=tmall/, '') + $(this).val();
    location.href = redirecturl;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM