Selector does not accept value=""

Asked

Viewed 86 times

4

I am trying to make my select empty after the previous select is modified. (Since the two are interconnected, where the select1 is state and as the state is released the cities in Select2

HTML code where the person selects the state, after selected state returns the cities of the selected state, and has the search button to search for items present in the respective city.

<div class="row rol-inp">
                    <div class="row input-form">
                    <div id="slct-estado">
                            <span class="blt"></span>
                            <select id="selectEstado" name="estado" onchange="javascript: getCitiesFromState(this.value)">
                                <option value="">Estado </option>
                                <?php foreach ($estados as $est) { ?>
                                <option value="<?php echo $est->UF; ?>"> <?php echo $est->UF; ?> </option>
                                <?php } ?>
                            </select>
                    </div>
                    </div>

                    <div class="row input-form">
                    <div id="slct-estado">
                            <span class="blt"></span>
                            <select id="cidadesSelect" name="cidades">
                                <option value=""> Cidade </option>
                            </select>
                    </div>
                    </div>

                    <div class="row borda">
                    <a onclick="getStoresFromCity(this.value)" class="bt-concluir"><strong>Encontrar</strong></a>    
                    </div>  
                </div>

Below, code that takes cities according to state, which already has value="", but is not working.

 function getCitiesFromState(estado) {

    document.querySelector('select[name=cities]').value="";

    $.ajax({
        url: ROOT + LANG + "/facebook/getCitiesFromState",
        dataType: "html",
        type: 'POST',
        data: {
            estado: estado
        },
        success: function (html) {
            $("#cidadesSelect").html(html);
        }

    });

}
  • How so empty? You want to prevent the user to change the status after it is selected?

  • Do you think your problem has to do with PHP or Java? Why are you using these tags?

2 answers

2

Instead of using the selector select[name=cities], use the same selector id of the select you are using to fill it, which is #cidadesSelect.

You can do it two ways:

Pure Javascript:

document.querySelector('#cidadesSelect').innerHTML = '';
ou
document.getElementById('cidadesSelect').innerHTML = '';

jQuery:

$('#cidadesSelect').empty();
ou
$('#cidadesSelect').html('');
ou
$('#cidadesSelect option').remove();
  • None of the solutions worked.

  • But Ajax is working normal?

  • Instead of using select[name=cities], use #cidadesSelect

  • Yes, it works normal

  • #citiesSelect also did not work brother.

  • Then there is another problem in your code that is not in the question rs. Because the methods to empty an element are exactly these.

  • The code has no problems. Is there any problem that we are not able to see.

  • If you are filling select, you should empty tb, since everything is in the same function... it doesn’t make sense that it doesn’t work.

  • Dude, the select has the property value yes, both for reading and writing. óia that

  • And Sam’s code is correct, if there are any errors, it is elsewhere in the code that is not posted in the question. Have you linked your role to any event or are calling it at any time?

  • @fernandosavio yes, you can assign value to select, but the use of its value should come from the selected option.

  • Oh yes... I commented more by the phrase that was removed in the edition. : D

Show 7 more comments

0

Forehead like this:

$('#seuSelect').empty();

*Note: Use the jQuery

  • I tried and it didn’t work either.

  • Hi Gabriel! Can you explain why "Use the jQuery"? I have contrary opinion, the jQuery in this case is cannon shot to catch a bird.

  • Sorry, I was going to write 'I used jQuery' and missed 'i'. Do as you see fit, the important thing is to work well ;)

Browser other questions tagged

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