2012-11-15 118 views
0

我一個使用jQuery的claviska選擇框 https://github.com/claviska/jquery-selectBoxclaviska/jquery-selectBox如何顯示默認值?

以下是代碼

<div class="section"> 
    <label>Location</label> 
    <div> 
     <select class="small selectBox" id="GeoLocation"style="display: none; padding: 6px;"> 
      <option value="1">London</option> 
      <option value="2">Madrid</option> 
      <option value="3">Paris</option> 
      <option value="4">New York</option> 
     </select> 
    </div> 
</div> 

我沒有問題,當列表是靜態的,適當地顯示在列表框中。 我的問題是,如何使用我從服務器端獲得的信息在選擇框中設置默認值。

默認情況下,我將'倫敦'作爲默認值。

支持我在JS var id=2(這是馬德里),我如何設置選擇框顯示'馬德里'作爲默認值?

我如何在JQuery中構建列表?

此之後被缺省情況下產生的(倫敦被選中)的HTML

<div> 
    <select class="small selectBox" id="GeoLocation" style="display: none; padding: 6px;"> 
     <option value="1">London</option> 
     <option value="2">Madrid</option> 
     <option value="3">Paris</option> 
     <option value="4">New York</option> 
    </select> 
    <a class="selectBox small selectBox-dropdown" style="width: 62px; display: inline-block;" title="" tabindex="0"> 
     <span class="selectBox-label" style="width: 35px;">London</span> 
     <span class="selectBox-arrow"></span></a> 
</div> 

回答

0

你可以試試這個方法

var id=2; 
// Select the option with the corresponding value.. 
// capture the text or html inside it 

var city = $('#GeoLocation option[value="'+ id+ '"]').text();​ 

// Set the text to the text in select  
$('.selectBox-label').text(city); 

Fiddle

+0

不工作,我已經編輯我的問題,謝謝 – user829174

+0

你到底想要達到什麼目標。你是否想用t填充selectBox-label span他選擇了項目 –

+0

在生成的示例中,您可以看到「倫敦」是用'selectBox-label'寫的。我想根據'val id = 2'將它替換爲'Madrid'。謝謝 – user829174