在下拉列表更改selectpicker不起作用

时间:2016-03-22 06:36:35

标签: javascript twitter-bootstrap

我正在使用bootstrap selectpicker。它在页面加载时工作正常,但在下拉列表更改时,它会给出selectpicker不是函数的错误。 的初始化

$(document).ready(function()
{
    $(".selectpicker").selectpicker();  
}

HTML

<div class="row">
    <div class="col-lg-6 col-sm-6 col-xs-12">
        <div class="form-group success">
            <label for="title"> Country <span class="asterisk">*</span> </label>

            <select data-size="5" onchange="changeCountry(this.value)" class="form-control selectpicker bs-select-hidden" name="Tours[countryid]" id="Tours_countryid">
                 <option value="">---Select Country---</option>
            </select>
        </div>
    </div>
    <div class="col-lg-6 col-sm-6 col-xs-12">
        <div class="form-group">
            <label for="widgettitle"> State <span class="asterisk">*</span> </label>
            <select id="Tours_stateid" name="Tours[stateid]" data-size="5" class="form-control selectpicker bs-select-hidden" maxlength="50">
                <option value="">---Select State---</option>
            </select>                                   
        </div>
    </div>
</div>

javascript onchange功能

function changeCountry(value)
        {
            $.ajax({
                url: "<?php echo Yii::app()->createurl('/site/changecountry') ?>",
                type: 'post',
                data: {id: value},
                success: function (result) {
                    $("#Tours_stateid").html(result)
                    $('.selectpicker').selectpicker('refresh');
                }
            });
        }

有一个下拉依赖项,即当我们更改国家/地区然后基于国家/地区时,将显示州列表

1 个答案:

答案 0 :(得分:0)

看起来工作正常。它只是你没有关闭选择元素。

<div class="row">
    <div class="col-lg-6 col-sm-6 col-xs-12">
        <div class="form-group success">
            <label for="title"> Country <span class="asterisk">*</span> </label>

            <select data-size="5" class="form-control selectpicker bs-select-hidden" name="Tours[countryid]" id="Tours_countryid">
                <option value="">---Select Country---</option><option value="1">Country1</option><option value="2">Country2</option>
            </select>

        </div>
    </div>
    <div class="col-lg-6 col-sm-6 col-xs-12">
        <div class="form-group">
            <label for="widgettitle"> State <span class="asterisk">*</span> </label>
            <select id="Tours_stateid" name="Tours[stateid]" data-size="5" class="form-control selectpicker bs-select-hidden" maxlength="50">
                <option value="">---Select State---</option>
            </select>                                   </div>
    </div>
</div>

在js文件中(使用jQuery更改事件来调用本地函数)

$(document).ready(function(){
    $('#Tours_countryid').on('change', function() {
        changeCountry(this.value);
    });
    function changeCountry(value) {
        console.log(value);
    }
});

希望有所帮助