这段代码是通过jquery动态增加限定数额的text(本例为5个) ,以及清除文本内容,用到了after()方法追加元素。
<script> <br>$(document).ready(function(){ <br>var spotMax = 5;//限定添加text元素的总个数 <br>var afterId = 0;//要追加元素的id <br>var newId=1;//新生成text的id <br>if($('table#vote').size() >= spotMax) {} <br>$("#btnAddOption").click(function(){ <br>afterId++; <br>newId=afterId+1; <br>addSpot(this, spotMax,afterId,newId); <br>}); <br>}); <br>//添加选项方法 <br>function addSpot(obj, sm,afterId,newId) { <br>if($('tr.spot').size() < sm){ <br>$('#vote_'+afterId).after( <br>'<tr id="vote_'+newId+'" class="spot"><th>'+afterId+'' + <br>'<td><input type="text" id="txtInput_'+afterId+'" class="input-text" value="" size="40" name="names" /> ' + <br>''); <br>$(":text[id^='txtInput_']").val("输入文本...");//给新增的文本赋予初始值 <br>} <br>else{ <br>alert("最多只能添加5项投票!"); <br>} <br>}; <br>//重置选项 <br>$("input#btnResetOption").click(function(){ <br>$(":text[id^='txtInput_']").val("");//清空文本内容 <br>}); <br></script>
以下是运行效果:










