這段代碼是通過jquery動態(tài)增加限定數(shù)額的text(本例為5個) ,以及清除文本內(nèi)容,用到了after()方法追加元素。
<!--以下為script代碼開始-->
復(fù)制代碼 代碼如下:
<script>
$(document).ready(function(){
var spotMax = 5;//限定添加text元素的總個數(shù)
var afterId = 0;//要追加元素的id
var newId=1;//新生成text的id
if($('table#vote').size() >= spotMax) {}
$("#btnAddOption").click(function(){
afterId++;
newId=afterId+1;
addSpot(this, spotMax,afterId,newId);
});
});
//添加選項(xiàng)方法
function addSpot(obj, sm,afterId,newId) {
if($('tr.spot').size() < sm){
$('#vote_'+afterId).after(
'<tr><th>'+afterId+'</th>' +
'<td><input type="text" value="" size="40" /></td> ' +
'</tr>');
$(":text[id^='txtInput_']").val("輸入文本...");//給新增的文本賦予初始值
}
else{
alert("最多只能添加5項(xiàng)投票!");
}
};
//重置選項(xiàng)
$("input#btnResetOption").click(function(){
$(":text[id^='txtInput_']").val("");//清空文本內(nèi)容
});
</script>
復(fù)制代碼 代碼如下:
<form method='post' action="admin/vote/doVote">
<table cellpadding=0 cellspacing=0>
<tr>
<th>投票名稱</th>
<td>
<input type="text" value="" size="85"/>
</td>
</tr>
<tr>
<th>投票描述</th>
<td>
<textarea cols="85" ></textarea>
</td>
</tr>
<tr>
<th>開始時間</th>
<td>
<input type="text" value="" size="40" readonly="readonly"/>
</td>
</tr>
<tr>
<th>結(jié)束時間</th>
<td>
<input type="text" size="40" readonly="readonly"/>
</td>
</tr>
<tr>
<th>是否多選</th>
<td>
<input type="radio" value="0" size="40"/>單選
<input type="radio" value="1" size="40"/>多選
</td>
</tr>
<tr>
<th>投票選項(xiàng)</th>
<td>
<input type="button" value="添加選項(xiàng)"/>
<input type="reset" value="重置選項(xiàng)"/>
</td>
</tr>
<tr>
<th></th>
<td>
<input type="submit" value="保存"/>
<input type="submit" value="取消"/>
</td>
</tr>
</table>
</form>
新聞熱點(diǎn)
疑難解答
圖片精選