HTML代碼:
<tr>
<td><fmt:message key="ttesbill.cusomerid"/></td>
<td><select name="cusomerid" class="cusomerid" style="width:165px">
<option>---請選擇---</option>
<c:forEach items="${bankList}" var="bankList">
<option value="${bankList.cusomerid}">${bankList.customerName} | ${bankList.cardno}</option>
</c:forEach>
</select>
</tr>
jQuery代碼:
$("select[name='cusomerid']").change(function(){
var cusomerid = $("option:selected", this).val(); // 擷取下拉框值
$.ajax({
type:"post", //使用Post提交請求
url:"${ctx}/ttesBillController.do?method=getCusomer", //提交URL
data:"cusomerid="+cusomerid, //提交參數
success:function(data,status){ //返回後的函數處理
if(status=="success"){ //確認傳回值
for(var p in data) //迴圈遍曆對象
$("input[name="+p+"]").val(data[p]); //填充HTML文字框內
}
},
error:function(){
alert("出錯了...您的操作有誤");
}
});
});
JAVA代碼:
public ModelAndView getCusomer(HttpServletRequest request,
HttpServletResponse response){
String cusomerid = request.getParameter("cusomerid");
TbankCustomer tBank = tbankCustomerService.getTbankCustomerById(cusomerid);
renderJson(response, JsonUtil.toString(tBank, JsonUtil.COLLECTION_FILTER));
//返回對象,不用轉換
return null;
}
使用Json返回boolean值
renderJson(response, toJson("success", flag));