Combobox jquery找到选定的val

时间:2012-03-15 00:25:16

标签: php jquery json forms

大家好我有一个带有jquery的组合框 - 但是当第一个选择选择alredy时,我不能填充第二个组合框。即时尝试获取值而不更改选择。

<form>
    <select name="tipo" id="Tipo_Id" class="buscaTiposVeiculos">
        <option value="1" selected="selected">teste1</option>
        <option value="2">teste2</option>
        <option value="3">teste3</option>
        <option value="4">teste4</option>
    </select>
    <select name="marca" class="recebeMarcas">
        <option value="">--- Select ---</option>
    </select>
</form>

jquery的

$('select.buscaTiposVeiculos').change(function () {
     $("select.recebeMarcas").html('<option value="">Carregando...</option>');

     // var opt = $("select.buscaTiposVeiculos"); tried like this
     // var val = $(this).val(); tried like this
     // var val = $(this).find('option:selected').val(); not works
     $('select.recebeMarcas >option').remove();
     $.post('/inc/geraCidades.php', {
         tipov: $(this).val(),
         tipo: "tipo"
     }, function (data) {

         $('select.recebeMarcas').html('<option value="">Selecione a Marca</option>' + data);
     });
 });

2 个答案:

答案 0 :(得分:1)

您选择的值是

$("#Tipo_Id option:selected").val();

答案 1 :(得分:0)

要在页面加载时触发更改事件,请自动填充第二个选择。可以删除一些注释代码

/* create the change handler  use ID is better selector for efficiancy*/
$('#Tipo_Id').change(function() {                                              
     /* this populates second select with one option*/
    $("select.recebeMarcas").html('<option value="">Carregando...</option>');                                          

   /* this removes option populated in line above*/
    $('select.recebeMarcas >option').remove();


    $.post('/inc/geraCidades.php', {tipov: $(this).val(), tipo : "tipo"}, function(data) {

        $('select.recebeMarcas').html('<option value="">Selecione a Marca</option>'+data);
    });

    /* trigger the change event on page load*/
}).change();