2012-12-28 42 views
0

我有一个列表框,其中itemValue是一个字符串,它显示的顺序是不准确的。我在$(document).ready(function(){$(“#item”)。val(1)上使用,并将项目的索引位置设置为列表中的第一个。默认情况下,它是设置为基于字母顺序的列表中的一个选项,这不是我想要的。Html Select Option

使用JQuery设置初始值索引我想工作,但是当我发布页面时,如果页面上有任何错误时。返回页面列表中的项目的价值追溯到值我在的document.ready()函数来设置我怎么能在走到这

代码;

jQuery的

$(document).ready(function(){ 

     $("#skinColorId").val(1); 
}); 

HTML:

数据从数据库数据的

<li> 
<form:label for="skinColorId" path="skinColorId">Select Skin Color</form:label>  
<form:select path="skinColorId" id="skinColorId" title="Select Skin Color"> 
<form:options items = "${skinColor.skinColorList}" itemValue="colorCode" itemLabel="colorDesc"/> 
</form:select>   
<form:errors path="skinColorId" class="errors"/><label class="colorPreviewer" id="skinColorPreviewer">color previewer</label> 
</li> 

样品来:

colorId | colorDesc |的ColorCode 155 ||选择颜色|| #FFFFFF 156 || ||水族#00FFFF 157 || ||蓝宝石#7FFFD4 158 || ||天青#F0FFFF 159 || ||米色#F5F5DC 160 || Bisque ||#FFE4C4 161 ||黑色||#000000 162 || BlanchedAlmond || #FFEBCD

+0

你可以提供你的代码,所以它会很容易帮助你.. – insomiac

+2

请给我们看一些标记和代码,以便我们可以帮助你。也许设置一个小提琴。 –

回答

3

尝试使用“selected”设置默认值。

对于如:

<option value="value" selected="selected">choose one</option> 

和张贴后...检查来自服务器的响应。

if (response.success) { 
    //clear the items value by using ($('#item').html('');}) 
else{ 
    //if response if failed... or some error occured 
    //then have the same value for your option tag 
} 

你真的不需要检查$(文件)。就绪既不设置在jQuery的价值...

试试吧...希望它的工作原理:)...