错误描述:
checkoutdetail.ftl页面:用Js中的Ajax方式提交form表单,CheckOutDetailWork.java中元素的值为空。
checkoutdetail.ftl提交表单部分代码:
<script>
$(function(){
$('#submit-order').click(function(){
//通过标签的名字获取元素列表
//var obj = document.getElementsByTagName("input");
//通过元素的名字获取元素group1的列表
var obj = document.getElementsByName("group1");
for(var i=0; i < obj.length; i++){
//逐个验证是否被选中
if(obj[i].checked){
//被选中的值
//alert(obj[i].value);
//若选中,就获取空间value的属性值
var checked = obj[i].value;
//通过value的属性值,到switch语句中设置隐藏域的值(选中为1,未选中为0)
switch(checked){
case "free":free = "1";continue;
case "local":local = "1";continue;
}
}else{
var checked = obj[i].value;
switch(checked){
case "free":free = "0";continue;
case "local":local = "0";continue;
}
}
}
//通过元素的名字获取元素group2的列表
var obj = document.getElementsByName("group2");
for(var i=0; i<obj.length; i++){
//选中为1,未选中为0
if(obj[i].checked){
var checked = obj[i].value;
switch(checked){
case "bankCard":bankCard = "1";continue;
case "alipay":alipay = "1";continue;
case "weChat":weChat = "1";continue;
case "direct":direct = "1";continue;
}
}else{
var unchecked = obj[i].value;
switch(unchecked){
case "bankCard":bankCard = "0";continue;
case "alipay":alipay = "0";continue;
case "weChat":weChat = "0";continue;
case "direct":direct = "0";continue;
}
}
}
$.ajax({
type: "POST",
url: '<@ofbizUrl>submitorder</@ofbizUrl>',
data: $('#submitorder').serialize(),
dataType: "json",
success: function(result){
if(result.code===1){
}
}
});
});
});
</script>
CheckOutDetailWork.java接收页面提交的值,部分代码
String group1 = request.getParameter("group1");
解决办法:
查看checkoutdetail.ftl页面,发现
<div class="radio-group">
<span style="white-space:pre"> </span><input id="free" class="le-radio" type="radio" <span style="color:#ff0000;">name="group1"</span> value="free"> <div class="radio-label bold">免运费</div><br>
<!-- 隐藏域传值,默认为非选中状态 -->
<input type="hidden" id="free" name="free" value="0">
<input id="local" class="le-radio" type="radio" <span style="color:#ff0000;">name="group1"</span> value="local" checked> <div class="radio-label">快递<br><span class="bold">$15</span></div>
<input type="hidden" id="local" name="local" value="1">
</div>
注意:form千万不能嵌套!!!