JavaScript code:
The code copy is as follows:
<script language="javascript">
function addrows(){
var len = optionlist.rows.length; //Get the number of rows of table
var obj = optionlist.insertRow(len);//Insert on the last line
/**Insert the first column**/
obj.insertCell(0);
obj.cells(0).innerHTML="Option" + (len+1) + ": <input type=text name=option size=28>";
}
function deleterow(){
var len = optionlist.rows.length;
if(len <= 1) {
alert("at least one option");
}
else {
optionlist.deleteRow(len-1);//Delete the last item
}
}
function getOptionCount(){
return optionlist.rows.length;
}
</script>
Key code in Jsp page
The code copy is as follows:
<input type="button" id="bt1" value="Add option" onClick="addrows();">
<input type="button" id="bt2" value="delete option" onClick="deletrow();">
Set the id of the table so that the table can be recognized in JavaScript
The code copy is as follows:
<table id="optionlist">
</table>