- 求助用Jquery實現,點擊添加選項新增一行
-
求助用Jquery實現,點擊添加選項新增一行,點擊後增加選項五,再點擊增加選項六
<table id="table_items" class="hovertable" style="width: 100%;" cellspacing="0"
cellpadding="0">
<tr id="tr">
<td class="table_th" >題目選項:
<td width="" id="contain"><input type="text" id="comment" name="comment"
maxlength="30" style="width: 80%" placeholder="選項一"/><font
color="red"> *</font>
<input type="text" id="title" name="title"
maxlength="30" style="width: 80%"" placeholder="選項二" />
<input type="text" id="commentx" name="comment"
maxlength="30" style="width: 80% " " placeholder="選項三" />
<input type="text" id="commentx" name="comment"
maxlength="30" style="width: 80% " placeholder="選項四" />
<div><a href="javascript:;" class="addRow" id="addText" onclick="javascript:add()">+添加選項</a></div>
</td>
</tr>
</table>
最佳回答:
<script>
function del(t){
$(t).parent('div').remove();
}
function add(){
var lastInput = $('#contain input:last');
var phName = lastInput.attr('placeholder');
phName = phName.replace("選項","");
if(phName=='一'){
phName='二'
}else if(phName=='二'){
phName='三'
}else if(phName=='三'){
phName='四'
}else if(phName=='四'){
phName='五'
}else if(phName=='五'){
phName='六'
}
$('<div class="inputs"><input input type="text" id="commentx" name="comment" maxlength="30" style="width: 80% " placeholder="選項'+phName+'" /><span onclick=del(this)>刪除</span></div>').insertAfter(lastInput.parent('div'));
}
</script>
<body>
<table id="table_items" class="hovertable" style="width: 100%;" cellspacing="0"
cellpadding="0">
<tr id="tr">
<td class="table_th" >題目選項:
<td width="" id="contain">
<div class='inputs'>
<input type="text" id="comment" name="comment"
maxlength="30" style="width: 80%" placeholder="選項一"/><font
color="red"> *<span onclick=del(this)>刪除</span></font></div>
<div class='inputs'><input type="text" id="title" name="title"
maxlength="30" style="width: 80%"" placeholder="選項二" /><span onclick=del(this)>刪除</span></div>
<div class='inputs'><input type="text" id="commentx" name="comment"
maxlength="30" style="width: 80% " " placeholder="選項三" /><span onclick=del(this)>刪除</span></div>
<div class='inputs'><input type="text" id="commentx" name="comment"
maxlength="30" style="width: 80% " placeholder="選項四" /><span onclick=del(this)>刪除</span></div>
<div><a href="javascript:;" class="addRow" id="addText" onclick="javascript:add()">+添加選項</a></div>
</td>
</tr>
</table>
</body>