在网址中附加参数以进行搜索过滤

时间:2019-04-25 16:13:07

标签: php jquery html5

嗨,我想在单击复选框时重写url参数值, 类似于亚马逊搜索。过滤 enter image description here  我尝试使用此代码

namespace TacticalAILib
{
public class CalculateFrontages
{

    static int[] U;

    private static TacLineStruct CalculateLines(List<MATEUnit> Army, double MetersPerPixel, int MSTGapValue)
    {

        int TempFrom, TempTo;
        float TempWeight;
        int NumEdges = 0;
        double Threshold = MetersPerPixel * MSTGapValue;
        U = new int[Army.Count];
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script language="javascript1.2">
$(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 ='http://localhost/sss.php?'+seasoning;
console.log('http://localhost/sss.php?'+seasoning);

  });

});

</script>

但它仅在控制台中运行 我无法修复它以url运行  我想要将参数附加到网址

1 个答案:

答案 0 :(得分:0)

  1. 如果您可以刷新窗口,则必须这样做

使用document.location.search += seasoning使页面刷新。

  1. 如果您不希望刷新窗口,则可以使用window.location pushState或replaceState。
window.history.replaceState(null, null, 'http://localhost/sss.php?'+seasoning);

window.history.replaceState(null, null, 'http://localhost/sss.php?'+seasoning);

$(document).ready(function () {
 $('input[type=checkbox]').click(function (e) {
  var seasoning = '', tempArray = [];
  $('input[name="vegetables[]"]:checked').each(function(){
      tempArray.push($(this).val());
  })
  console.log(tempArray);
  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();
  }
  console.log('http://localhost/sss.php?'+seasoning);
 });
});
<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>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>