当前位置 : 主页 > 编程语言 > java >

jsp – 为什么Spring MVC在路径上附加一个“1”来生成表单id:checkbox?

来源:互联网 收集:自由互联 发布时间:2021-06-25
如果我在jsp中有这样的复选框: form:checkbox path =“agreeToLegalAgreements”/ 它导致: input id =“agreeToLegalAgreements1”name =“agreeToLegalAgreements”type =“checkbox”value =“true”/ input type =“hidde
如果我在jsp中有这样的复选框:
< form:checkbox path =“agreeToLegalAgreements”/>

它导致:
< input id =“agreeToLegalAgreements1”name =“agreeToLegalAgreements”type =“checkbox”value =“true”/>< input type =“hidden”name =“_ agreeToLegalAgreements”value =“on”/>

为什么ID会附加“1”?我问的原因是因为如果我想使用javascript选择此复选框,我必须硬编码“1”:
.的document.getElementById( ‘agreeToLegalAgreements1’)检查= TRUE;

这是必要的,因为您可能需要将多个复选框绑定到同一个字段,并且每个复选框都需要具有唯一ID.

例如,如果表单对象具有兴趣列表

Programming: <form:checkbox path="interests" value="Programming"/>
Painting: <form:checkbox path="interests" value="Painting"/>
Fishing: <form:checkbox path="interests" value="Fishing"/>

输出将是:

Programming: <input id="interests1" name="interests" type="checkbox" value="Programming"/>
Painting: <input id="interests2" name="interests" type="checkbox" value="Painting"/>
Fishing: <input id="interests3" name="interests" type="checkbox" value="Fishing"/>

(我省略了隐藏的空值输入)

网友评论