2014-04-08 107 views
1

我需要从下拉按钮编程(JavaScript的)选择一个元素以编程方式选择的一种元素:从下拉按钮

  <div class="form-group"> 
      <label class="col-lg-3 control-label">Button</label> 
      <div class="col-lg-9"> 
       <div class="btn-group m-r"> 
       <button data-toggle="dropdown" class="btn btn-md btn-default dropdown-toggle"> 
        <span class="dropdown-label" data-placeholder="Please select">Option 1</span> 
        <span class="caret"></span> 
       </button> 
       <ul class="dropdown-menu dropdown-select"> 
        <li id="properties_form_1" class="active"><a href="#"><input type="radio" name="d-s-c-1">Option 1</a></li> 
        <li id="properties_form_2"><a href="#"><input type="radio" name="d-s-c-1">xxx</a></li> 
        <li id="properties_form_3"><a href="#"><input type="radio" name="d-s-c-1">xxxx</a></li> 
        <li id="properties_form_4"><a href="#"><input type="radio" name="d-s-c-1">xxx</a></li> 
        <li id="properties_form_5"><a href="#"><input type="radio" name="d-s-c-1">xxxxx-Pair</a></li> 
       </ul> 
       </div> 
      </div> 
      </div> 

[UPDATE] 我曾尝试以下:

order_form_div.find("[id^='properties_form_']").each(function(){ 
    console.log("id: " + $(this).attr('id')); 
    $(this).removeClass('active'); 
}); 
order_form_div.find("#" + item.productType).addClass('active'); 

它会使正确的项目处于活动状态,但“选项1”中的文本不会更新。所以,不是手动改变这个文本,我认为有任何优雅的方法来做到这一点。

+1

您是否尝试过任何操作? – j08691

+4

*“我需要以编程方式(javascript)从下拉按钮中选择一个元素:”*意味着什么? – epascarello

+0

不知道你在问什么/你想在这里。 –

回答

0

一个瞎猜:

// deactivate all 
$('.dropdown-menu li').removeClass('active'); 

// the id of the element to select 
var selected = 'properties_form_2'; 

// set active class 
$('#' + selected).addClass('active'); 
// check radio-button 
$('#' + selected + '.active input').prop('checked', true); 
2

一旦你打开选定的属性给你希望的选项被选中,运行

$('.dropdown-menu').selectpicker('refresh'); 

这应该更新的按钮。 (可能需要在btn-group上运行它,从您的结构中不确定)