我有一个 JSP 页面,其代码如下:
<div id="TextBoxesGroup"> <div class="control-group">
<label class="control-label" for="inputAuto">Room type</label>
<div class="controls">
<!-- <input type="text" id="inputAuto" class="grd-white" /> -->
<s:select id="inputAuto" data-form="select2" cssStyle="width:200px" data-placeholder="Select Room Type" name="resortRoomtype" headerKey="-1" list="roomtypeList"/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputAuto">Room Numbers</label>
<div class="controls">
<!-- <input type="text" id="inputAuto" class="grd-white" /> -->
<s:textfield id="inputAuto" cssClass="grd-white" name="roomnumbers" onkeypress="return isNumber(event)"/>
<s:label name="roomNumberError" cssClass="help-inline" cssStyle=" color: red;"></s:label>
</div>
</div>
</div>
<div >
<input type='button' value='+' id='addButton' Class="btn " />
<input type='button' value='-' id='removeButton' Class="btn" />
</div>
我用于添加和删除按钮的 js 是::
<script type="text/javascript">
$(document).ready(function () {
$("#addButton").click(function () {
if( ($('#TextBoxesGroup').children().size()+1) > 12) {
alert("Resort don't have more rooms" );
return false;
}
var id = ($('#TextBoxesGroup').children().size()+1).toString();
$('#TextBoxesGroup').append('<div class="control-group" id="control-group">
<label class="control-label" for="inputSelectGroup">Room Type</label>
<div class="controls"><select style="width:200px" id="inputSelectGroup" name="resortRoomtype" data-form="select2">
<option value="Single Room">Single Room</option>
<option value="Double Room">Double Room</option>
<option value="Twin Room">Twin Room</option>
<option value="Interconnecting Room">Interconnecting Room</option>
<option value="Suite">Suite</option>
<option value="Pent House Suite">Pent House Suite</option>
</select>
</div>
</div>
<div class="control-group" id="control-group">
<label class="control-label" for="inputauto">Number of rooms</label>
<div class="controls">
<input type="text" Class="grd-white" id="inputauto" name="roomnumbers"
onkeypress="return isNumber(event)" >
</div>
</div>');
});
$("#removeButton").click(function () {
if ($('#TextBoxesGroup').children().size() == 2) {
alert("No more textbox to remove");
return false;
}
$('#TextBoxesGroup').children().last().remove();
$('#TextBoxesGroup').children().last().remove();
});
});
</script>
在我的行动课上我有::
private List<Integer> roomnumbers;
getter/setters;
我有一个验证方法,它将验证是否有任何字段为空。如果为空,那么它将返回 INPUT,我将返回我的表单页面(例如 add_resort.jsp)。所以对于前我输入了一个房间类型::单人房间房间号是::2,房间类型::双人房间房间号是::空(我忘了输入数字)然后我点击按钮保存更改和验证发生..但是当我在房间号文本字段中返回该页面时,我得到了类似这样的值::: [2,null]
测试用例::::::
验证前房间号 =2
验证后 roomnumber=[2]
测试用例::::::
验证前 roomnumber =2 roomnumber= null
验证后 roomnumber=[2,null]
在验证方法中
if(roomnumbers != null){
roomnumbers.add(new Integer(55));
setRoomnumbers(roomnumbers);
addFieldError("roomnumbers", "Please Enter valid room number.");
}