2017-04-06 104 views
-1

我在頁面中放置了兩個select2控件。一個在桌子外面,另一個在裏面。通過下面的代碼,只有表格外的工作正常,但內部沒有。 (單擊控件時沒有任何反應)。Select2控件不能在HTML表格中工作

HTML

<select id="drpBizPartner" class="form-control select2 select2-hidden-accessible" ng-model="vm.salesInvoice.cardCode" ng-options="x.id as x.cardName for x in vm.businessPartners" name="CardCode" style="width: 100%;"> 
    <option value="0">Select Business Partner</option> 
</select> //Outside the table 

<select id="drpItem" class="form-control select2 select2-hidden-accessible" ng-model="lineItem.item" ng-options="item as item.itemCode for item in vm.items" name="ItemCode" style="width: 100%;"> 
    <option value="0">Select Item</option> 
</select> //Inside the html table 

的Javascript

 $(function() { 
      $('#drpBizPartner').select2(); 
      $('#drpItem').select2(); 
     }); 

搜索互聯網後,我發現了我認爲是一個修復得到select2控制工作。劇本如下:

 $table.on('post-body.bs.table', function() { 
      $('#drpItem').select2(); 
     }); 

但是,上面的代碼清除select2控件(外部和內部)上的項目。請我如何讓他們都能完美地工作?非常感謝。

回答

1
<table> 
    <tr> 
    <td> <div id="drpItem" ></div></td> 
    </tr> 
</table>. 

//您的js代碼將是

$('#drpItem').select2(); 
+1

雖然此代碼段可以解決的問題,[包括一個解釋](http://meta.stackexchange.com/questions/114762/explaining - 完全基於代碼的答案)確實有助於提高發布的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – Ferrrmolina

+0

thanx Ferrrmolina for suggetion,我馬上改進了。這是我的第一篇文章 –

+0

沒問題,祝你有個愉快的一天! – Ferrrmolina