【问题标题】:Bootstrap-select not working with dynamically populated dropdownBootstrap-select 不适用于动态填充的下拉菜单
【发布时间】:2017-03-31 07:54:05
【问题描述】:

我的表单如下所示:

<form action="/submit/" method="post" >{% csrf_token %}
<div class="col-lg-4 col-lg-offset-4">
<div class="form-group has-feedback">
<label class="control-label" for="inputSuccess2">Enter twitter username</label>
<input type="text" class="form-control" id="inputSuccess2", name='user'/>
<span class="glyphicon glyphicon-search form-control-feedback"></span>
<div class="text-center">

<label class="control-label" for="inputSuccess2">Select Job Type</label><br>
<select  class="selectpicker" id="type" name="type"></select>
<br>
<div class="btn-group">
    <select  name="specialization" id="specialization"></select>
    <br/></div>
    <script language="javascript">
        populateCountries("type", "specialization");
    </script>
    <br/>
    <input class="btn btn-primary" type="submit" value="Submit">
    </div>
    </div>
    </div>
</form>

下拉菜单的 js 文件如下所示:

var country_arr = new Array("Social Service and Education","Business and Sales","Language and Arts","Engineering and Technology", "Information Technology" );

// States
var s_a = new Array();
s_a[0] = "Please Select One";
s_a[1]="Counselor|School or Career Counselor|Community Service Manager|Social Worker|Preschool Teacher|Librarian|Professor|Psychologist";
s_a[2]="Sales Agent|Real Estate Agent|Accountant|Budget Analyst|Cost Estimator|Sales Engineer";
s_a[3]="Graphic Designer|Musician|Editor|Photographer|Translator|Artist";
s_a[4]="Chemical Engineer|Civil Engineer|Electronics Engineer|Environmental Engineer|Mechanical Engineer";
s_a[5]="Computer Programmer|Database Administrator|Software Developer|Web Developer";


function populateStates(countryElementId, stateElementId) {

    var selectedCountryIndex = document.getElementById(countryElementId).selectedIndex;

    var stateElement = document.getElementById(stateElementId);

    stateElement.length = 0; // Fixed by Julian Woods
    stateElement.options[0] = new Option('Select Specialization', '');
    stateElement.selectedIndex = 0;

    var state_arr = s_a[selectedCountryIndex].split("|");

    for (var i = 0; i < state_arr.length; i++) {
        stateElement.options[stateElement.length] = new Option(state_arr[i], state_arr[i]);
    }
}

function populateCountries(countryElementId, stateElementId) {
    // given the id of the <select> tag as function argument, it inserts <option> tags
    var countryElement = document.getElementById(countryElementId);
    countryElement.length = 0;
    countryElement.options[0] = new Option('Select Job Type', '-1');
    countryElement.selectedIndex = 0;
    for (var i = 0; i < country_arr.length; i++) {
        countryElement.options[countryElement.length] = new Option(country_arr[i], country_arr[i]);
    }
    $countryElementy.selectpicker('refresh');
    // Assigned all countries. Now assign event listener for the states.

    if (stateElementId) {
        countryElement.onchange = function () {
            populateStates(countryElementId, stateElementId);
        };
    }
}

但是,下拉菜单不起作用。如果我删除 selectpicker 类,我会失去样式。

有没有办法可以将引导样式与此代码一起使用?

【问题讨论】:

  • Bootstrap select 的样式为.form-control,或者是您用来使其成为.dropdown 的某个插件?

标签: css django twitter-bootstrap bootstrap-select


【解决方案1】:

为什么需要使用 javascript 来填充选择?

只需将必要的值保存在视图中的列表中,然后在模板中的选择中打印它们。

示例:

<select id="id_select" name="select">
    <option value="">your select</option>
    {% for value in yourdata %}
            <option value="{{value}}">{{ value }}</option>
        {% endif %}
    {% endfor %}
</select>

并使用引导样式:)

【讨论】:

  • 因为我的第一个下拉菜单的选择决定了第二个下拉菜单中显示的内容
  • 填充所有数据并让jquery显示所需的值?您将从头开始加载所有数据,jquery 只需在您的 html 中添加 disabled 属性
  • 你可以在这里找到一些提示stackoverflow.com/questions/22955253/…你只需要知道什么时候启用和禁用选择:)
猜你喜欢
  • 2014-07-17
  • 2015-12-17
  • 1970-01-01
  • 2016-09-12
  • 2020-08-10
  • 2012-09-03
  • 1970-01-01
  • 2021-06-20
  • 2020-12-19
相关资源
最近更新 更多