Javascript 引导表单帮助程序-在国家/地区选择器上动态选择国家/地区
我正在使用引导表单帮助程序中的国家/地区选择器: 问题是我需要用一个国家动态初始化它。我使用datacountry=US作为默认值,无论如何,我需要在documentready函数中更改它Javascript 引导表单帮助程序-在国家/地区选择器上动态选择国家/地区,javascript,jquery,html-select,bootstrap-form-helper,Javascript,Jquery,Html Select,Bootstrap Form Helper,我正在使用引导表单帮助程序中的国家/地区选择器: 问题是我需要用一个国家动态初始化它。我使用datacountry=US作为默认值,无论如何,我需要在documentready函数中更改它 <div id="country-select" class="bfh-selectbox bfh-countries" data-flags="true" data-value="US"> <input type="hidden" value="">
<div id="country-select" class="bfh-selectbox bfh-countries" data-flags="true" data-value="US">
<input type="hidden" value="">
<a class="bfh-selectbox-toggle" role="button" data-toggle="bfh-selectbox" href="#">
<span class="bfh-selectbox-option input-medium" data-option=""></span>
<b class="caret"></b>
</a>
<div class="bfh-selectbox-options">
<input type="text" class="bfh-selectbox-filter">
<div role="listbox">
<ul role="option">
</ul>
</div>
</div>
</div>
从中,您可以使用
$(document).ready(function(){
$('#country-select').bfhcountries({country: 'TN'});
});
$'LoadCountry'。单击函数{
$'countries1'.bfhccountries{country:'TN'};
};
装货国
使用下面的html选择初始化时的默认国家/地区。另外,将bootstrap-formhelpers.js和bootstrap-formhelpers.css添加到html中,否则您的选择将不会填充数据
<form><select id="country-select" name="country-select" class="form-control bfh-countries" data-country="US" data-flags="true"></select></form>
有关初始化的更多选项,请参阅。
这是一个例子
根据OP的评论,OP使用了一个主题,使用,引用
选择2将侦听它所附加到的元素上的更改事件。当您进行任何需要在Select2中反映的外部更改(如更改值)时,您应该触发此事件
从而反映变化,
$('#country-select').val('TN').trigger('change');
你的html是错误的。这不是countrypicker的初始化方式。这是初始化后的html,是您应该如何初始化它的。也要清楚你想做什么。有两件事你说你需要做我如何初始化它?如果我使用您提供的代码,则select为空。我的代码运行良好。您是否将bootstrap-formhelpers.js包含在html中?您还需要将该select放在表单标记中,尽管这一点不用说,因为您使用的是bootstrap插件。在小提琴中看到这一点,我不能用:$'country-select'.bfhccountries{country:'TN'}来选择一个国家;已经尝试过了,我得到了:Uncaught TypeError:$..bfhcountries不是一个函数您需要在页面中包含bootstrap-formhelpers.js和bootstrap-formhelpers.css。我已经包含了它们。你能给我一把js小提琴吗?我不能让它工作,我要mad@Razinar检查上面的代码片段!不可能。。。即使触发了更改事件,select仍保持在美国。请显示一个示例页面,以便我们可以进一步移动,否则这是正确的方法。解决了这个问题,我调用了一个Elixir view函数,在呈现时填充数据国家。不管怎样,我会把你的答案标为公认的答案,因为它适用于那些没有冲突的人
$('#country-select').val('TN').trigger('change');