使用jquery添加或删除多个检查表项到另一个列表
Add or Remove Multiple checklist items to another list using jquery
我有一个2div与多个复选框和2按钮添加和删除。检查一些项目并单击添加,我想将它们更新到第二个div,没有重复的值和相同的删除过程。
我试着这样做,但不能使它正确。我尝试了如下操作
$(document).ready(function() {
$('#btn-add').click(function(){
$('input[type="checkbox"]:checked').each(function() {
$('.chk-container-Add').append("<li><input class="checkbox2" type="checkbox" value='"+$(this).val()+"'>"+$(this).text()+"</li>");
$(this).remove();
// Has to be added in div2 and removed from div1
});
});
$('#btn-remove').click(function(){
//Has to remove from div2 and to be added in div1
});
});
这是我的小提琴演示
http://jsfiddle.net/M_Sabya/xuktz0j7/3/当您想要移动包含复选框的li
项时,您正在尝试单独移动复选框—或者更确切地说,重新创建复选框。
$('#btn-add').click(function() {
$('.chk-container input[type="checkbox"]:checked').each(function() {
$(this).
prop('checked', false).
closest('li').appendTo($('.chk-container-Add'));
});
});
$('#btn-remove').click(function() {
$('.chk-container-Add input[type="checkbox"]:checked').each(function() {
$(this).
// uncheck as we move
prop('checked', false).
// find the parent <li>
// append to the other container
// no need to remove
closest('li').appendTo($('.chk-container'));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="div1">
<ul class="chk-container">
<li>
<input class="ck1" type="checkbox" value="item1">Item 1</li>
<li>
<input class="ck1" type="checkbox" value="item2">Item 2</li>
<li>
<input class="ck1" type="checkbox" value="item3">Item 3</li>
<li>
<input class="ck1" type="checkbox" value="item4">Item 4</li>
<li>
<input class="ck1" type="checkbox" value="item5">Item 5</li>
<li>
<input class="ck1" type="checkbox" value="item6">Item 6</li>
</ul>
</div>
<button id="btn-add">Add »</button>
<button id="btn-remove">« Remove</button>
<div id="div2">
<ul class="chk-container-Add">
<li>
<input class="ck1" type="checkbox" value="item7">Item 7</li>
<li>
<input class="ck1" type="checkbox" value="item8">Item 8</li>
</ul>
</div>
您应该使用.parent()
,因为$(this)
是复选框本身。
还有,你使用了双引号,这会导致语法错误:
.append("<li><input class="checkbox2" type="checkbox" ...
应该转义字符串中的双引号,或者使用单引号,如下所示:
.append("<li><input class='checkbox2' type='checkbox' ...
$(document).ready(function() {
$('#btn-add').click(function(){
$('#div1 input[type="checkbox"]:checked').each(function() {
$('.chk-container-Add').append("<li><input class='checkbox2' type='checkbox' value='"+$(this).val()+"'>"+$(this).parent().text()+"</li>");
$(this).parent().remove();
});
});
// it's basicaly the same as the above, except of the container, which is "div2"
$('#btn-remove').click(function(){
$('#div2 input[type="checkbox"]:checked').each(function() {
$('.chk-container').append("<li><input class='checkbox2' type='checkbox' value='"+$(this).val()+"'>"+$(this).parent().text()+"</li>");
$(this).parent().remove();
});
});
});
<<p> JSFiddle演示/strong> 相关文章:
- 使用javascript将动态表从一个html页面打印到另一个html页
- 通过单击动态加载的表中同一行的另一个字段来更新一行的字段
- 将一个表的id与另一个表中的user_id进行匹配,以获取mysql中的记录
- 循环遍历包含另一个表单的表单
- 使用javascript将两个文本框值相加到表中的另一个文本框中
- 如何在javascript转换编码中将一个动态值从一个表单添加到另一个页面
- 选中复选框的Jquery/Javascript问题使用输入框操作将行从一个表添加到另一个表
- 如何检查一个字符串的所有字符是否都存在于另一个字符串中
- 尝试简化检查对象键是否为true并将其推送到另一个对象
- 从一个iframe到另一个ifame的过帐表单-IE
- 从id标记中的文本进行检查,并将类添加到另一个标记中
- 当单元格内容由于forumula从另一个工作表编译数据而更改时发送电子邮件
- 检查一个元素是否有一个类与另一个类总是返回true
- 在另一个可观察量完成后触发第二个表单提交单击
- 如何使用JavaScript在另一个页面上填写表单
- 包括来自另一个页面的Boostrap模态;t验证表单
- 如何通过 JS 将内容从一个表单中的一个文本框复制到另一个表单中的另一个文本框
- 使用jquery添加或删除多个检查表项到另一个列表
- 在移动到另一个表单之前,检查是否有未保存的表单
- Javascript -检查一个表中的值并更新另一个表