【发布时间】:2017-06-12 07:10:24
【问题描述】:
我有一个产品类别的下拉菜单,其中显示了数据库中的一组值。
为此,我使用 Spring 标签。 I want one of the options to display a value called "Others" and when that value is selected I am displaying a textbox to accept a new product category.
问题是当我提交表单时,product_category 被设置为“其他,(新添加的类别)”。但我真正想要的只是新添加的类别。
有没有办法做到这一点?
我的代码如下:
添加.jsp
<form:form action="${actionURL}" method="POST" modelAttribute="Record">
..................
..................
<div class="row">
<section class="col col-4">
<label class="label">Product Category</label> <label
class="select"> <form:select id="product_category"
path="product_category" onchange="add_new_productCategory();">
<form:option value="">Choose category</form:option>
<form:options items="${productOptions}"
itemValue="product_category" itemLabel="product_category" />
<form:option itemLabel="Others" value="Others"/>
</form:select><i></i>
</label>
</section>
<section class="col col-4">
<label class="label">New Category</label> <label class="input">
<form:input type="text" path="product_category"
id="new_product_category" disabled="disabled" required="required" />
</label>
</section>
</div>
............................
...................
<input type="submit" value="Submit" class="button">
.............................
我正在使用以下脚本
function() {
if ($("#product_category").val() == 'Others') {
$('#new_product_category').prop('disabled', false);
} else {
$('#new_product_category').prop('disabled', 'disabled');
}
};
选择“其他”选项时将执行
在我的模型类中,这只是像
这样的常规过程 @Column(name = "PRODUCT_CATEGORY")
private String product_category;
在我的控制器中
@RequestMapping(value = "/add.html", method = RequestMethod.GET)
public String getRegisterPage(Model model) {
System.out.println("-----------------add records ---------------------");
model.addAttribute("Record", new RecordParams());
List<ProductCategory> productOptions = listProductParamsService.findProductCategories();
model.addAttribute("productOptions", productOptions);
return "add";
}
和
@RequestMapping(value = "/add.html", method = RequestMethod.POST)
public String saveRecord(@ModelAttribute("Record") RecordParams recordParams) {
System.out.println(recordParams);
RegisterService.addRecord(recordParams);
return "redirect:/add.html";
}
一切都按预期工作,除了产品类别,当打印时显示为
RecordParams [id=null,................., product_category=Others,IPTL,....]
我应该怎么做才能纠正这个问题,请帮忙?
提前致谢。
【问题讨论】:
标签: java spring-mvc modelattribute spring-form