how to clean an attribute in html by javascript [duplicate]

0

I have a problem in my code where it takes a value from the database related to the value of the combobox selected by the user and fills in another combobox, until it is working, however when the user of an onchange again it adds the values instead of overwriting I tried to use the reset () function and clear () of jquery did not work follow the code below:

<script>
$(document).ready(function() {
    $("#button").change(function() {
        var valor = $("#button").val();
        alert(valor);
        $.post("procura.php",
            {valor: valor},
                function(data){
                    alert(data);
                    var resultado = data.split(",");
                    for ( var i = 0 ; i < resultado.length ; i++ ){
                        var option = $("<option></option>").appendTo($('#result'));
                        option.attr("value", resultado);
                        option.html(resultado[i]);
                    }
                    document.getElementById("form").reset();
                    });
                });
            });
</script>
</head>
<body>

<select id="button">
    <option value="AVT">AVT</option>
    <option value="MMFT">MMFT</option>
    <option value="RUNIN">RUNIN</option>
</select>
<select id="result" name="result">
    <option value=""></option>
</select>
    
asked by anonymous 09.10.2018 / 15:24

2 answers

0

Friend tries to reset the options of your select before the new fill:

$("#button").change(function () {
        var valor = $("#button").val();
        alert(valor);
        $.post("procura.php",
            { valor: valor },
            function (data) {
                alert(data);
                var resultado = data.split(",");
                $('#result').empty();
                for (var i = 0; i < resultado.length; i++) {
                    var option = $("<option></option>").appendTo($('#result'));
                    option.attr("value", resultado);
                    option.html(resultado[i]);
                }
            });
    });

So whenever a new database value comes up, it clears your combobox and adds the values again. I hope I have helped friend.

    
09.10.2018 / 15:32
0

Just clear the select with id="result".

 $("#result").empty();
 //Aqui segue seu código
 var option = $("<option></option>").appendTo($('#result'));
    
09.10.2018 / 15:41