2012-12-28 40 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的價值...

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