简体   繁体   English

基于开关的URL重定向

[英]Switch based url redirection

I'm having a problem get this piece of code to work: 我在使这段代码起作用时遇到问题:

 $(document).ready(function(){
     $('#ButtonAluguel').click(function(){
     { 
       var id = $(this).attr('name');
       var str = "";
       $("option:selected").each(function () {
         switch(id=='Trololo'){
         case true:
            var option = $(this);
            str += '?tid_1[]='+ option.attr('value');
            break;   
        case false:
            var option = $(this);
            str += '?tid[]='+ option.attr('value');break;
       }
      });
      window.location = "localhost/aluguel"+ str;
    }});
  });

I need it to keep adding stuff to "str" based on the name of a multiple select, identified above as id. 我需要它根据多选名称(上面标识为id)继续向“ str”添加内容。 Long story short, if the name of the select is "Trololo", id adds tid_1[], if not, it adds tid[] to the str. 长话短说,如果选择的名称是“ Trololo”,则id将添加tid_1 [],否则不将其添加到str。 Any help is appreciated. 任何帮助表示赞赏。

Edit: The multiple select code is as follows(Forgot to put it in the first place, the question doesn't make much sense without it) 编辑:多重选择代码如下(忘记将其放在第一位,没有它的问题就没有多大意义了)

     <form >
<select class="SelectTipoAluguel"  multiple="true" data-placeholder="Tipo de Imóvel" style="width:200px;">
        <option value="1">Aasdasdasd</option>
        <option value="2">B</option>
        <option value="3">C</option>
        <option value="4">D</option>
</select>
<select name="Trololo" class="SelectBairroAluguel" id="trololo" multiple="true" data-placeholder="Bairro" style="width:200px;">
        <option value="1">Aadasd</option>
        <option value="2">Basda</option>
        <option value="3">Casda</option>
        <option value="4">Dasda</option>
</select>
<input class="ButtonSubmitHome" id="ButtonAluguel" value="Pesquisar" >
  </form>

To explain it more clearly, the user must fill the form and choose between the options, so when he clicks the "ButtonAluguel" , every option from the select "SelectTipoAluguel" is added to the URL as tid[] and the ones from "SelectBairroAluguel" is added to the URL as tid_1[] 为了更清楚地解释它,用户必须填写表格和选项之间进行选择,所以他点击时"ButtonAluguel" ,从选择的每一个选项"SelectTipoAluguel"被添加到URL为tid[]并从那些"SelectBairroAluguel"作为tid_1[]添加到URL

Code edited to reference updated question and OP's comments. 编辑代码以引用更新的问题和OP的注释。

 $(document).ready(function() { var id; var str = ""; $('#ButtonAluguel').click(function() { var option = []; $('select option:selected').each(function(i, selected) { id = $(this).parent().attr('name'); option[i] = $(selected).val(); if (id == 'Trololo') { str += '?tid_1[]=' + option[i]; } else { str += '?tid[]=' + option[i]; } }); var url = "localhost/aluguel" + str; console.log(url); //window.location = "localhost/aluguel" + str; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="SelectTipoAluguel" multiple="true" data-placeholder="Tipo de Imóvel" style="width:200px;"> <option value="1">Aasdasdasd</option> <option value="2">B</option> <option value="3">C</option> <option value="4">D</option> </select> <select name="Trololo" class="SelectBairroAluguel" id="trololo" multiple="true" data-placeholder="Bairro" style="width:200px;"> <option value="1">Aadasd</option> <option value="2">Basda</option> <option value="3">Casda</option> <option value="4">Dasda</option> </select> <input class="ButtonSubmitHome" id="ButtonAluguel" value="Pesquisar"> 

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

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