2011-05-27 79 views
0

我有一個問題,我必須動態生成下拉框使用jQuery和彈簧綁定它們。我在我的表單支持對象中有一個列表,並試圖使用遞增的計數器來綁定它。JQuery動態選擇彈簧綁定

我收到follwoing錯誤:

org.springframework.beans.InvalidPropertyException: Invalid property 'selectedPermntRestrictCat[ + categoryCounter + ]' of bean class [com.portal.policymanagement.formObject.EditPolicyFormObject]: Invalid index in property path 'selectedPermntRestrictCat[ + categoryCounter + ]'; nested exception is java.lang.NumberFormatException: For input string: " + categoryCounter + "

$(document).ready(function(){ 
    var categoryCounter=0; 
    $('#addCategory').click(function() { 
     $('<div class="holder"><div class="left"><label> </label><form:select path="policy.selectedPermntRestrictCat[' + categoryCounter + ']"><form:option value="0" label="Select" /><form:option value="6" label="Entertainment" /><form:option value="7" label="Religion" /><form:option value="8" label="Weapons" /><form:option value="9" label="Virtual Community" /><form:option value="10" label="Hacking" /><form:option value="11" label="Search Engines" /><form:option value="12" label="Educational" /><form:option value="13" label="Other" /><form:option value="14" label="TEST NON_EXISTANT" /><form:option value="1" label="Violence" /><form:option value="2" label="Drugs" /><form:option value="3" label="Adult Content" /><form:option value="4" label="Online Resources" /><form:option value="5" label="Gambling" /></form:select></div><div class="right"><a id="'+i+'" class="but_default" href="#">Delete</a></div><div class="errors left"></div><div class="clear"></div></div>').fadeIn('slow').appendTo('.blockedCategories'); 
     categoryCounter++; 
    }); 
}); 

回答

1

,因爲你混合使用客戶端邏輯(jQuery的)服務器端邏輯(Spring標籤)這個代碼是沒有意義的。您看到的錯誤是由於作爲服務器端標記的<form:select>在呈現頁面期間在Javascript文字內執行而造成的。

爲了解決你的任務,你需要看正常的頁面呈現在你的<form:select>生成的HTML和與jQuery代碼創建類似HTML,這將是這樣的:

$('<div class="holder"><div class="left"><label> </label><select name="policy.selectedPermntRestrictCat[' + categoryCounter + ']"><option value="0" label="Select" />...</select>...').fadeIn('slow').appendTo('.blockedCategories');