2
我想在選擇國家後顯示國家。STRUTS2和AJAX - 選擇國家,填寫國家
JSP
<s:select label="COUNTRY" name="summaryData.addressCountry" id="addForm_countryCode"
list="loyaltyCountryMap" tabindex="" headerKey="US" headerValue="United States"
listKey="key" listValue="value.countryName"
onchange="getStateList('#addForm_countryCode')">
</s:select>
<s:select label="STATE" name="summaryData.addressCityCode" headerValue="-Select-" headerKey="-Select-" list="stateList" required="true" cssClass="storedPaymentInput_size1 required" id="stateList"/>
JAVASCRIPT:
function getStateList() {
var countryCode = $('#addForm_countryCode").val();
$.ajax({
url: 'ajaxStateList',
dataType: 'html',
data: { countryCode : countryCode},
success: function(data) {
$('#stateList').html(data);
}
});
}
struts.xml中
<action name="ajaxStateList" class="actions.AjaxStateList">
<result name="success"/>
</action>
操作類
private List<String> stateList;
private String countryCode;
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public List<String> getStateList() {
return stateList;
}
public void setStateList(List<String> stateList) {
this.stateList = stateList;
}
public String execute() {
LoyaltyStateProvinces.getInstance();
stateList = StateProvinces.getAllStateProvinceByCountryCode(countryCode);
for(StateProvince state: states){
stateList.add(state.getStateProvinceCode());
}
return SUCCESS;
}
如何使用AJAX + Struts2工作?
+1僅僅用於使用jQuery而不使用那些可憐的ajax標記庫。 – Quaternion 2012-04-30 08:37:08