单击后,将子项与输入字段类似树追加
After click append child with input field like tree
我正在尝试创建具有左子级和右子级的组织结构。就像这个演示http://www.jqueryscript.net/demo/Create-An-Editable-Organization-Chart-with-jQuery-orgChart-Plugin/
但我想用输入字段保存这里的数据。我想在单击后为每个创建左子项和右子项。现在所有的显示在底部一个接一个,只删除按钮。我想要添加按钮也为每个输入表单创建子项。这里我用的像这个
$(function() {
var scntDiv = $('#p_scents');
var i = $('#p_scents p').size() + 1;
$('#addScnt').live('click', function() {
$('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i + '" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
i++;
return false;
});
$('#remScnt').live('click', function() {
if (i > 2) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
* {
font-family: Arial;
}
h2 {
padding: 0 0 5px 5px;
}
h2 a {
color: #224f99;
}
a {
color: #999;
text-decoration: none;
}
a:hover {
color: #802727;
}
p {
padding: 0 0 5px 0;
}
input {
padding: 5px;
border: 1px solid #999;
border-radius: 4px;
-moz-border-radius: 4px;
-web-kit-border-radius: 4px;
-khtml-border-radius: 4px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>
<div id="p_scents">
<p>
<label for="p_scnts">
<input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" />
</label>
</p>
</div>
这是jsfiddle DEMO我想用输入表单创建与插件相同的树结构。但没能做到。我的代码片段没有问题,所以我放置了jsfiddle链接。
代码中存在各种错误,主要是id应该是唯一的。并且使用on()
而不是live()
,因为它在新版本中已折旧。你可以用表格做这样的事情,这是一个简单的演示,你需要更新它
// bind click event handler
$("#main").on('click', '.add', function() {
//getting parent td
var $td = $(this).parent();
// creating child element
var $td1 = $('<td>', {
html: '<input /> <button class="add">+</button> <button class="remove">-</button>'
});
// getting child table if there is
var $tbl = $td.children('table')
// checking child exist or not , if yes then append child to it
if ($tbl.length)
$tbl.find('tr').eq(0).append($td1);
// else create new table and update
else
$td.append($('<table>', {
html: $('<tr>', {
html: $td1
})
}))
// bind remove handler
}).on('click', '.remove', function() {
// remove the parent td
$(this).parent().remove();
});
input {
width: 20px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="main" style="width:100%;text-align:center">
<tr>
<td>
<input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" />
<button class="add">
+
</button>
<button class="remove">
-
</button>
</td>
</tr>
</table>
这可能会完成任务,但我承认有一些错误,但我认为它仍然值得一看。。我是在Bootstrap网格系统的帮助下完成的。
HTML
<div class="container-fluid">
<div class="row">
<div class="parent-col col-md-12">
<input type="text" />
<BR />
<a class='sibling'>Add Sibling</a>
or
<a class='child'>Add Child</a>
</div>
</div>
</div>
JS-
$('.child').click(function(){
var r = $(this).closest('div.row').clone(true, true);
r.find('.parent-col').not(':eq(0)').remove();
var c = $(this).closest('.parent-col');
var n = c.append( r );
});
$('.sibling').click(function(){
var thisC = $(this).closest('.parent-col');
var n = thisC.clone(true, true);
thisC.after(n);
var c = $(this).closest('div.row');
var len = $('div.parent-col', c).length;
var newClass = Math.ceil(12/len);
c.find('.parent-col').attr('class', 'parent-col col-md-'+newClass);
});
jsFiddle
在全宽视图中测试它,可以很容易地注意到树结构。
元素是动态添加到页面中的,因此事件不会绑定到添加的元素。
检查此代码。它会起作用的。
$(function() {
var scntDiv = $('#p_scents');
var i = $('#p_scents p').size() + 1;
$('#addScnt').on('click', function() {
$('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
i++;
return false;
});
$(document).on("click", '#remScnt', function(e) {
if( i > 2 ) {
$(this).closest('p').remove();
i--;
}
return false;
});
});
* { font-family:Arial; }
h2 { padding:0 0 5px 5px; }
h2 a { color: #224f99; }
a { color:#999; text-decoration: none; }
a:hover { color:#802727; }
p { padding:0 0 5px 0; }
input { padding:5px; border:1px solid #999; border-radius:4px; -moz-border-radius:4px; -web-kit-border-radius:4px; -khtml-border-radius:4px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>
<div id="p_scents">
<p>
<label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" /></label>
</p>
</div>
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 而循环只设置php中输入字段中的第一个值
- 在输入字段中将最小金额设置为
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- 使用jQuery'生成输入字段;s追加
- jQuery-根据值在输入字段后追加图像
- 单击后,将子项与输入字段类似树追加
- RethinkDB:条件追加启动(仅当特定字段匹配时)
- 将隐藏字段追加到 DIV 容器中
- 爪哒追加() 和动态隐藏字段
- 将新字段追加到窗体
- 如何在隐藏字段中追加和删除复选框值
- 如何在JQuery中将元素追加到动态字段中
- 当选项包含空格时,在select下拉列表中动态追加值会在下拉列表中追加空字段
- 如何将文件追加到文件输入字段中
- 想提交表单元素,同时通过追加按钮创建字段
- 如何在嵌套数组中追加新字段
- 如何排除字段为";追加销售的";在Mongoose
- Yii2隐藏输入字段使用javascript追加,并从控制器获得它
- 如何在单击时将文本追加到输入字段