开发者

Get select selected value

I have a url like this: index.php/home/lista_modelo_ajax?id="+$('#colecoes').val(),

The thing is that the: $('#colecoes').val() is always the first item of the select, not the one I choose.

Any ideas?

Thanks in advance.

EDIT

<script>
    $(function() {
            $( "#modelos" ).autocomplete({
                source: "<?php echo base_url();?>index.php/home/lista_modelo_ajax?id="+$('#colecoes').val(),
                minLength: 2
            });
    });
    </script>


<label for="colecoes">Coleções</label>
        <select name="colecoes" id="colecoes">
            <option value="16">-</option>
            <?php foreach($lista_colecao as $colecao){?>
            <option value="<?php echo $colecao-&g开发者_开发问答t;idColecao;?>"><?php echo $colecao->nomeColecao;?></option>
            <?php }?>
        </select>
        <label for="modelos">Modelo</label>
        <input type="text" name="modelos" id="modelos" />

All php is working fine, generating the IDs on the value of options objects, the problem is only with JS


This will depend on where you are writing this code. If you are writing it directly in the document ready that would be normal as at the moment this code executes that's the selected value. On the other hand if you are writing it for example in the .change event of the dropdown list you will get the actual value:

$('#colecoes').change(function() {
    var id = $(this).val(); // now you will get the actual selected value
    $.ajax({
        url: 'index.php/home/lista_modelo_ajax',
        data: { id: id },
        success: function(result) {
            ...
        }
    });
});


Just do:

$('#colecoes').val(); //assuming `colecoes` is the id of the select

Example with onchange:

$('#colecoes').change(function(){
    alert(this.value); //alerts the selected option
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜