2012-11-15 44 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