JavaScript code:
Copy Code code as follows:
<script language= "JavaScript" >
function Addrows () {
var len = optionlist.rows.length; Get the number of rows in a table
var obj = Optionlist.insertrow (len);//insert on last line
/** inserts the first column **/
Obj.insertcell (0);
Obj.cells (0). innerhtml= "Options" + (len+1) + ": <input type=text name=option";
}
function DeleteRow () {
var len = optionlist.rows.length;
if (len <= 1) {
Alert ("Must have at least one option");
}
else {
Optionlist.deleterow (len-1);//Delete last item
}
}
function Getoptioncount () {
return optionlist.rows.length;
}
</script>
Key code in a JSP page
Copy Code code as follows:
<input type= "button" id= "BT1" value= "add Option" onclick= "addrows ();" >
<input type= "button" id= "BT2" value= "delete option" onclick= "deleterow ();" >
Set the ID of the table so that the table can be recognized in JavaScript
Copy Code code as follows:
<table id= "Optionlist" >
</table>