Automatically select a select and press the button

Asked

Viewed 1,892 times

3

I got a field like <select>/<option> that when the user selects a value I call a function to popular another <select>, type the state and city combos, this is working, and then the user clicks on a button that makes an ajax call to bring some data to display.

What I need and I don’t know is when I get this first one <select> only one value, already selected, so make the call for that second select and already press the button even without the user select this second select.

For example, I have a select from another city state and a button. By selecting a state the user can already press the button or select a city and press the button, only when there is only one state already selected this state and also already trigger this button, giving only the user the option to select a city and then press the button.

I hope I haven’t been too confused and you can understand so you can help me.

<label for="estado">
<select>
    <option value="0"> ---Selecione--- </option>
    <option value="SP"> São Paulo </option>
</select>

<label for="cidade">
<select>
    <option value="0"> ---Selecione--- </option>
</select>

<button type="submit" id="listarItens">Buscar</button>

In this case if you only have the State of São Paulo already select and trigger the Search button, remembering that I have a function created that when the user selects the state calls this function.

$(document).ready(function() {
    changeSelect('estado', ['cidade'], 'selecionaCidades.php');

    $('#listarItens').click(function(event){
        event.preventDefault();
        //aqui executa algumas coisas como um ajax, mas isso acho que não preciso listar correto
    });


});

I don’t know if I need to pass all structure of this changeSelect function right.

  • Can you put the code you already have? easier to understand, and answer...

  • 2

    You can use this example to re-create what you want?: http://jsfiddle.net/xv9Lg/

  • @Sergio already added an example of what I need. I hope you help me and help me.

  • Marcelo, all selects have one <option value="0"> ---Selecione--- </option> ?

  • Yes, there is @Sergio

  • Okay. I left an answer below. That’s what you’re looking for?

Show 1 more comment

1 answer

1


Test use so to know if a select has only one option:

$('select').each(function () {
    var options = $(this).find('option');
    var qtdOptions = options.length;
    if (qtdOptions == 2)  {      // uso dois porque a sua primeira option é só uma guia
        this.selectedIndex = 1;  // escolher a segunda option
        $("select[name='estado']").change();
        $('#listarItens').trigger('click');  // carregar no botão!
    }
});

Example

This code searches all selects and if any have only one option (maybe wanted to use ==2 since it has the neutral text ---Selecione---). If you do not want to search all selects the code can be simpler.

  • So I understand what you sent, thank you, in case I just need you to look at select state itself, thence I left $('select[name=cliente]').each(function () { but when I do that I should have already executed changeSelect but didn’t call, have any idea how I can do?

  • @Marcelodiniz, which is the select name=cliente ? If you’re just one, you don’t need each()

  • so I made an updated not your example. See if you can understand me. http://jsfiddle.net/9MWTH/1/

  • @Marcelodiniz, calls yes. I added logs to your code. Know how to see? I also leave a chat link open if you want to ask questions. In the meantime, check the console: http://jsfiddle.net/9MWTH/2/

  • It actually calls only when you choose some option in the state select, not in that correct automatic call.

  • @Marcelodiniz, line 2 of your code is the function call

  • That’s right @Sergio. Why, I’m doing it wrong?

Show 2 more comments

Browser other questions tagged

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