- button doesn't work and if clicked + button,then add double rows. how to modify my code. thank youhtml code
<td>
<span>
<input type="text" name="document_sign_user_order" style="width:20px;"/>
<select name="fk_dept_seq">
<option>--------</option>
<!--{@ depts }-->
<option value="{.dept_seq}">{=_(.dept_name)}</option>
<!--{/}-->
</select>
<select name="fk_position_seq">
<option>--------</option>
<!--{@ positions }-->
<option value="{.position_seq}">{=_(.position_name)}</option>
<!--{/}-->
</select> <select name="fk_user_seq">
<option></option>
<!--{@ users }-->
<option value="{.user_seq}">{=_(.user_name)} [{=_(.dept_name)}]</option>
<!--{/}-->
</select>
<button class="btn_del_opt">-</button>
</span>
<button class="btn_add_opt">+</button>
</td>jquery code (function($){// var btn_del = $('button.btn_del_opt');
// var btn_add = $('button.btn_add_opt');
function del_opt(obj)
{
if($(obj).parent().parent().next().children('td').children('span').length == 1)
{
return alert('삭제하실수 없습니다.');
}
$(obj).parent().parent().next().last().remove();
cont_btn();
}
function add_opt(obj)
{
console.log($(obj));
$(obj).parent().parent().after($(obj).closest('tr').clone());
$(obj).parent().parent().next().children('td').children('span').children('input').val('');
cont_btn();
}
function cont_btn()
{
$('button.btn_del_opt').map(function(k,v){
$(v).click(function(){
del_opt(this);
});
});
$('button.btn_add_opt').map(function(k,v){
$(v).click(function(){
add_opt(this);
});
});
}
cont_btn();
})(jQuery);
<td>
<span>
<input type="text" name="document_sign_user_order" style="width:20px;"/>
<select name="fk_dept_seq">
<option>--------</option>
<!--{@ depts }-->
<option value="{.dept_seq}">{=_(.dept_name)}</option>
<!--{/}-->
</select>
<select name="fk_position_seq">
<option>--------</option>
<!--{@ positions }-->
<option value="{.position_seq}">{=_(.position_name)}</option>
<!--{/}-->
</select> <select name="fk_user_seq">
<option></option>
<!--{@ users }-->
<option value="{.user_seq}">{=_(.user_name)} [{=_(.dept_name)}]</option>
<!--{/}-->
</select>
<button class="btn_del_opt">-</button>
</span>
<button class="btn_add_opt">+</button>
</td>jquery code (function($){// var btn_del = $('button.btn_del_opt');
// var btn_add = $('button.btn_add_opt');
function del_opt(obj)
{
if($(obj).parent().parent().next().children('td').children('span').length == 1)
{
return alert('삭제하실수 없습니다.');
}
$(obj).parent().parent().next().last().remove();
cont_btn();
}
function add_opt(obj)
{
console.log($(obj));
$(obj).parent().parent().after($(obj).closest('tr').clone());
$(obj).parent().parent().next().children('td').children('span').children('input').val('');
cont_btn();
}
function cont_btn()
{
$('button.btn_del_opt').map(function(k,v){
$(v).click(function(){
del_opt(this);
});
});
$('button.btn_add_opt').map(function(k,v){
$(v).click(function(){
add_opt(this);
});
});
}
cont_btn();
})(jQuery);
{
....
cont_btn(); //这里为什么又要调用?
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title> new document </title>
<meta name="keywords" content="">
<meta name="description" content="">
<script type="text/javascript" src="js/jq.js"></script> </head> <body>
<table>
<tr>
<td>
<span>
<input type="text" name="document_sign_user_order" style="width:20px;"/><select name="fk_dept_seq">
<option>--------</option>
<!--{@ depts }-->
<option value="{.dept_seq}">{=_(.dept_name)}</option>
<!--{/}-->
</select><select name="fk_position_seq">
<option>--------</option>
<!--{@ positions }-->
<option value="{.position_seq}">{=_(.position_name)}</option>
<!--{/}-->
</select><select name="fk_user_seq">
<option></option>
<!--{@ users }-->
<option value="{.user_seq}">{=_(.user_name)} [{=_(.dept_name)}]</option>
<!--{/}-->
</select>
<button class="btn_del_opt">-</button>
</span>
<button class="btn_add_opt">+</button>
</td>
</tr>
</table>
<script type="text/javascript">
<!--
(function($){// var btn_del = $('button.btn_del_opt');
// var btn_add = $('button.btn_add_opt');function del_opt(obj)
{
if($(obj).parent().parent().next().children('td').children('span').length == 1)
{
return alert('sdfds');
}
$(obj).parent().parent().parent().next().last().remove();
//cont_btn();
}function add_opt(obj)
{
//console.log($(obj));
$(obj).parent().parent().after($(obj).closest('tr').clone());
$(obj).parent().parent().next().children('td').children('span').children('input').val('');
//cont_btn();
}function cont_btn()
{
$('button.btn_del_opt').map(function(k,v){
$(v).click(function(){
del_opt(this);
});
});
$('button.btn_add_opt').map(function(k,v){
$(v).click(function(){
add_opt(this);
});
});
}cont_btn();})(jQuery);//-->
</script>
</body>
</html>