Java 如果选择了特定的,则限制从数据绑定到modelAttribute
我有一个产品类别的下拉列表,其中显示了数据库中的一组值 为此,我使用了Spring标签。我希望其中一个选项显示一个名为“其他”的值,当选择该值时,我将显示一个文本框以接受新的产品类别 问题是当我提交表单时,产品类别被设置为其他,新添加的类别。但我真正想要的只是新添加的类别 有没有办法做到这一点 我的代码如下: add.jspJava 如果选择了特定的,则限制从数据绑定到modelAttribute,java,spring-mvc,modelattribute,spring-form,Java,Spring Mvc,Modelattribute,Spring Form,我有一个产品类别的下拉列表,其中显示了数据库中的一组值 为此,我使用了Spring标签。我希望其中一个选项显示一个名为“其他”的值,当选择该值时,我将显示一个文本框以接受新的产品类别 问题是当我提交表单时,产品类别被设置为其他,新添加的类别。但我真正想要的只是新添加的类别 有没有办法做到这一点 我的代码如下: add.jsp <form:form action="${actionURL}" method="POST" modelAttribute="Record"> .......
<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">
.............................
当选择“其他”选项时,将执行该选项
在我的模型课上,这只是一个通常的过程,就像
@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";
}
除产品类别外,所有功能均按预期工作,当打印显示为
RecordParams [id=null,................., product_category=Others,IPTL,....]
我该怎么做才能纠正这个问题,请帮忙
提前谢谢。如果我了解您的问题,请尝试按照以下建议进行操作,这可能会对您有所帮助: 如果要提交单个产品类别, 如果产品类别值为“其他”,则禁用产品类别并启用新产品类别,反之亦然
function() {
if ($("#product_category").val() == 'Others') {
$('#product_category').prop('disabled', true); // disabled
$('#new_product_category').prop('disabled', false);
}
//else {
//$('#new_product_category').prop('disabled', 'disabled');
// $('#product_category').prop('disabled', false);
// $('#new_product_category').prop('disabled', true); //disabled
//}
};
在这里,一旦您禁用了product_类别,您就无法更改其值,因此其他部分无效,
若要再次启用,请在新产品类别的focusout或blur上编写另一个函数(如果其值为空)
更新:
另一种方法是,在表单提交中选中$'product_category'.val。如果是其他人禁用它。
不要忘记为$new\u product\u category添加必需属性true。感谢所有帮助过您的人。我找到了更好的方法 我在模型中引入了一个新领域
@Transient
private String select_product_category;
然后将所选值保存到此字段,并在发现其值为空时将此值设置为product_category
我对代码做了以下更改
<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="select_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>
............................
...................
现在它工作得很好。这是不可能做到的,也不是一个好的做法。如果我禁用了“产品类别”选择框,如果用户意外单击了其他人并希望选择其他选项,用户将如何返回并选择其他选项?如果用户仅单击其他人$'new\u product\u category'。聚焦,并为此元素写入聚焦,如果有人再次将其留空,请启用$'product\u category'@santaram\u感谢您的意见。我还找到了另一种解决方法。请核对答案
@Transient
private String select_product_category;
<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="select_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>
............................
...................
@Transient
private String select_product_category;
@Column(name = "PRODUCT_CATEGORY")
private String product_category;
-----------------------------
public String getSelect_product_category() {
return select_product_category;
}
public void setSelect_product_category(String select_product_category) {
if (!select_product_category.equals("Others")) {
setProduct_category(select_product_category);
} else {
this.select_product_category = select_product_category;
}
}
public String getProduct_category() {
return product_category;
}
public void setProduct_category(String product_category) {
if (product_category.equals(null)) {
this.product_category = getSelect_product_category();
} else {
this.product_category = product_category;
}
}