2015-07-11 97 views
0

我遇到了问题。 我试过用自举形式助手做了选择框。(http://js.nicdn.de/bootstrap/formhelpers/docs/state.htmlSelectBox问题(国家/州)

所以我得到了“国家”一选择框和其他的“状态”

<select id="countries" class="form-control bfh-countries"></select> 
<select id="states" class="form-control bfh-states" data-country="countries"></select> 

如果让我选择一个价值形态国家框出现在州框中,所以一切都OK。

但是,如果我用一个脚本

<button onclick="test()" class="btn">Load Country</button> 

function test(){ 
      document.getElementById('countries').value = "AU"; 
     } 

没有什么附加的“状态”选择框,我应该得到国家的经功能试验()正确选择的状态?

我能做些什么来解决这个问题?

http://jsfiddle.net/xf8ech7k/1/

在此先感谢

回答

0

的问题有,你是不是triggerent “变” 的事件。

看看你修改code

function test(){ 
 
    var select = document.getElementById('countries') 
 
    , event = document.createEvent("UIEvents"); 
 
    select.value = "AU"; 
 
    event.initUIEvent("change", true, true); 
 
    select.dispatchEvent(event); 
 
}

然后,看看这个post

由于@CBroe说,你可以通过一个简单的方式做到这一点做:

$("select").val("AU").trigger("change"); 
+1

既然bootstrap已经使用了jQuery,那可以做得简单得多,'$('#countries')。val('AU')。trigger('change');' – CBroe

+0

非常感谢,我一直在搜索2天: (并感谢链接! 干杯 – Pierre

+0

@Mindastic我试图用选择框中的一个标志来修改我的表单,但是当我触发该值时,标志不出现,我刚刚获得了该值。在这种情况下有什么补充吗? http://jsfiddle.net/xf8ech7k/2/ – Pierre