How to search with . serialize()

Asked

Viewed 147 times

2

I have this Search/Serialize Jsfiddle, only I didn’t want him to take the name of select, only the value of option and search.

It can "ignore" the name and use only the value ?

Here’s the code:

    var host = "http://www.rs1.com.br/";
    $(document).ready(function(){
        $('#send').click(function(){
            var variables = $('#form').find('campo').serialize();
            var destination = host + variables.replace('=','/');
            $('#link').html(destination)
            window.open(destination);      
        });
    });

<form id="form">
    <select>
        <option value="tutto-moto" class="campo">Tutto</option>
    </select>
    <select>
        <option value="Acessorio" class="campo">Tutto</option>
    </select>    
    <select>
        <option value="produto" class="campo">Iphone+4s</option>
    </select>   
</form>
<button type="button" id="send" class="btn">Buscar</button>
  • You want something like http://www.rs1.com.br/tutto-moto/Acessorio/produto that’s it?

  • That’s right, I didn’t leave in the exact order to bring the results but that’s the idea.

1 answer

3


My knowledge of JS/jQuery is not so broad, but you can do so Jsfiddle.

Basically just scroll through the elements instead of using serialize, and treat the string. Now you have how to do it with serialize I don’t know.

$('#send').click(function(){
    var variables = '';
    // Percorre todos os campos campos dentro da tag select com a classe campo
    $('#form select').find('.campo').each(function(){
       variables += $(this).attr('value')+'/'; // Concatena o valor do option
    });
    var destination = host + variables;
    $('#link').html(destination)
    window.open(destination);
});
  • That’s exactly what I wanted haha :D Thank you, just tell me how I do so that the search button does the search inside the same page without opening another ? 'Cause the way he’s opening up a new flap.

  • 1

    Usa windows.location.href = destination; instead of window.open(destination);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.