【问题标题】:Append parameter in url on multiple checkbox group click在多个复选框组单击的 url 中附加参数
【发布时间】:2017-07-08 11:51:13
【问题描述】:

我想在单击复选框时重写 url 参数值,类似于 LinkedIn Advance 搜索。

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
  <script type='text/javascript'>
  
  $(document).ready(function () {
$('input[type=checkbox]').click(function (e) {
var seasoning = jQuery.map($(':checkbox[id=seasoning\\[\\]]:checked'), function (n, i) {return n.value;}).join(',');

window.location ='example.com?seasoning='+seasoning;
 
});

});
</script>
<h3>Filter recepies:</h3>

<div>
<p>Select vegetables</p>
<label><input type="checkbox" id="vegetables[]" value="potato"> Potato</label><br>
<label><input type="checkbox" id="vegetables[]" value="onion"> Onion</label><br>
<label><input type="checkbox" id="vegetables[]" value="tomato"> Tomato</label><br>
</div>

<div>
<p>Select seasoning</p>
<label><input type="checkbox" id="seasoning[]" value="salt"> Salt</label><br>
<label><input type="checkbox" id="seasoning[]" value="pepper"> Pepper</label><br>
<label><input type="checkbox" id="seasoning[]" value="chilli"> Chilli Flakes</label><br>
</div>

我想要的结果

Click1: 当我从蔬菜中点击 Potato 时,我的 URL 应该如下所示
example.com?vegitables=potato

Click2: 当我从蔬菜中点击 Onion 时,我的 URL 应该如下所示
example.com?vegitables=potato,onion

Click3: 当我点击调味盐时,我的 URL 应该看起来像
example.com?vegitables=potato,onion&seasoning=salt

Click4:当我点击辣椒调味时,我的网址应该是这样的
example.com?vegitables=potato,onion&seasoning=salt,pepper

【问题讨论】:

  • 检查我发布的答案,正是你需要的。

标签: javascript jquery url post


【解决方案1】:

你可以这样做

$(document).ready(function () {
  $('input[type=checkbox]').click(function (e) {
  var seasoning = '', tempArray = [];
  $('input[name="vegetables[]"]:checked').each(function(){
      tempArray.push($(this).val());
  })
  if(tempArray.length !== 0){
     seasoning+='vegetables='+tempArray.toString();
     tempArray = [];
  }
  
  $('input[name="seasoning[]"]:checked').each(function(){
      tempArray.push($(this).val());
  })
  if(tempArray.length !== 0){
     seasoning+='&seasoning='+tempArray.toString();
  }
 
 // window.location ='example.com?seasoning='+seasoning;
 console.log('example.com?'+seasoning);

  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<p>Select vegetables</p>
<label><input type="checkbox" name="vegetables[]" value="potato"> Potato</label><br>
<label><input type="checkbox" name="vegetables[]" value="onion"> Onion</label><br>
<label><input type="checkbox" name="vegetables[]" value="tomato"> Tomato</label><br>
</div>

<div>
<p>Select seasoning</p>
<label><input type="checkbox" name="seasoning[]" value="salt"> Salt</label><br>
<label><input type="checkbox" name="seasoning[]" value="pepper"> Pepper</label><br>
<label><input type="checkbox" name="seasoning[]" value="chilli"> Chilli Flakes</label><br>
</div>

为简单起见和进一步编辑,这里是工作JSFIDDLE的链接

【讨论】:

  • 这对 2 参数(蔬菜、调味料)来说就像一个魅力!如果我为 param3 $('input[name="param3[]"]:checked').each(function(){ tempArray.push($ (this).val()); }) if(tempArray.length !== 0){ seasoning+='&param3='+tempArray.toString(); }
猜你喜欢
  • 2013-05-08
  • 2011-06-09
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 2012-01-24
相关资源
最近更新 更多