将选中的值移动到另一个块并将它们附加到
我想将选定的列表项从一个块(块 1)移动到另一个块(块 2)。移动到块 2 后,如果我从块 2 中删除该项目,它应该附加回块 1。
这是我的代码:
HTML:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<table>
<tr>
<td>
<div id="left">
<ul>
<li><input type="checkbox" value="Option 1" onclick="addval(this)" />Option 1</li>
<li><input type="checkbox" value="Option 2" onclick="addval(this)" />Option 2</li>
</ul>
</div>
</td>
<td>
<div id="right">
<ul>
</ul>
</div>
</td>
</tr>
</table>
</body>
</html>
脚本:
function addval(x){
values1 = $(x).val();
$('#right ul').append("<li><img src='http://www.msshifi.com/skin/frontend/msshifi/default/images/delete-icon.gif' name="+ values1 +" onclick='removeval(this,values1)'></img>"+ values1 +"</li>");
$(x).parent().remove();
}
function removeval(y,val){
values2 = val;
$('#left ul').append("<li><input type='checkbox' value="+ values2 +" name="+ values2 +" onclick='addval(this)' />"+ values2 +"</li>");
$(y).parent().remove();
}
CSS:
div { border:1px #ccc solid; height:100px; overflow:auto; width:200px; background:#f1f1f1; }
ul { margin:0; padding:0; }
ul li { list-style:none; }
可以在线获取该项目的工作演示: http://jsfiddle.net/prajan55/maLqV/
请帮忙..提前致谢。
I want to move the selected list items from one block (block 1) another block (block 2). After moving to block 2, if I delete the item from block 2, it should append back to block 1.
Here is my code:
The HTML:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<table>
<tr>
<td>
<div id="left">
<ul>
<li><input type="checkbox" value="Option 1" onclick="addval(this)" />Option 1</li>
<li><input type="checkbox" value="Option 2" onclick="addval(this)" />Option 2</li>
</ul>
</div>
</td>
<td>
<div id="right">
<ul>
</ul>
</div>
</td>
</tr>
</table>
</body>
</html>
The script:
function addval(x){
values1 = $(x).val();
$('#right ul').append("<li><img src='http://www.msshifi.com/skin/frontend/msshifi/default/images/delete-icon.gif' name="+ values1 +" onclick='removeval(this,values1)'></img>"+ values1 +"</li>");
$(x).parent().remove();
}
function removeval(y,val){
values2 = val;
$('#left ul').append("<li><input type='checkbox' value="+ values2 +" name="+ values2 +" onclick='addval(this)' />"+ values2 +"</li>");
$(y).parent().remove();
}
The CSS:
div { border:1px #ccc solid; height:100px; overflow:auto; width:200px; background:#f1f1f1; }
ul { margin:0; padding:0; }
ul li { list-style:none; }
Working demo of the same is available online at : http://jsfiddle.net/prajan55/maLqV/
Kindly help.. thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你只是错过了一些引言。
看这里: http://jsfiddle.net/maLqV/7/
固定代码:
You're just missing a few quotes.
Look here: http://jsfiddle.net/maLqV/7/
Fixed code:
我可以建议一个稍微不同的实现:
JS Fiddle demo。
这保留了复选框和文本的详细信息,而您的实现似乎删除了标签或标签中的数字(这似乎已由 @sje397的答案),而且此版本还允许从元素的
onclick
属性中删除单击处理程序。这可能是也可能不是奖金。May I suggest a slightly different implementation:
JS Fiddle demo.
This preserves the details of the checkbox and text, whereas your implementation seemed to remove the label, or the number from the label (which seems to have been resolved by @sje397's answer), but also this version allows for the click handler to be removed from the
onclick
attribute of the elements. Which may, or may not, be a bonus.