dongxian7471
2017-11-09 21:32
采纳率: 100%
浏览 59
已采纳

使用JQuery填充选择

I have the following select html:

<div class="form-group col-lg-6 row">
   {{ Form::select( 'state',array( 0 => '' ),null, array( 'class' => 'form-control', 'data-placeholder' => 'State','id' => 'state' ) ) }}
</div>

or

<select class="form-control" id="state" data-placeholder="Selecione">
</select>

I want populate with Bootstrap + Chosen using the following code in JQuery:

$(document).ready( function () {
            var token = $('#token').val();
            var estados = $('#state');

            $.ajax({
                url : './estados',
                dataType : 'json',
                type : 'post',
                data : {
                    _token : token
                },
                success : function ( data ) {

                    estados.find('option').remove();
                    estados.append( $('<option>').val( 0 ).text( '' ));
                    //console.log( data );
                    $.each(data, function (i, j) {
                        console.log( j.sigla+" "+j.descricao );
                        var option  = $('<option>').val( j.sigla ).text( j.descricao ) ;
                        estados.append( option );
                    });
                    estados.trigger('chosen:updated');
                }
            });
        });

But doesn't work. If I look in console.log show the data successfully.

At PHP I can populate, but I want use with laravel.

[EDIT 1]

The result from data is:

AC Acre
AL Alagoas
AP Amapá
AM Amazonas
BA Bahia
CE Ceará
DF Distrito Federal
ES Espírito Santo
GO Goiás
MA Maranhão
MT Mato Grosso
MS Mato Grosso do Sul
MG Minas Gerais
PA Pará
PB Paraíba
PR Paraná
PE Pernambuco
PI Piauí
RJ Rio de Janeiro
RN Rio Grande do Norte
RS Rio Grande do Sul
RO Rondônia
RR Roraima
SC Santa Catarina
SP São Paulo
SE Sergipe
TO Tocantins

enter image description here

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题