JSP页面修改属性后验证不通过再恢复成初始值
在JSP页面里修改一个对象的属性,如果验证不通过再恢复成初始值。 <td> <input type="text" id="firstMoney" name="firstMoney" value="${contract.firstMoney}" onchange="checkMoney()" />元</td>
/验证金额是否为数字function checkMoney(){ var strP =/^\d+(\.\d+)?$/; var firstMoney=document.getElementById("firstMoney").value; if(firstMoney!=null && firstMoney!=""){ if(!strP.test(firstMoney)) {alert("金额必须为数字!");document.getElementById("firstMoney").value = "${contract.firstMoney}";//貌似JS也识别一定程度的EL表达式。另我很吃惊document.getElementById("firstMoney").focus();return false; } }
如果是用<c:forEach></c:forEach>的话,在遍历一个集合,修改这个集合中的某个对象的属性,用上面那个方法就不行了。参照以下即可:把值传到JS方法。或者用隐藏域把老值保存下也行。onchange=“abc(${list.aount})”function abc(a){if(isNaN(document.getElementById("").value)){document.getElementById("").value=a;}}