jQuery 和多选
在我的页面上,我必须选择 $.post 函数加载的选项值。我有一个按钮可以将数据从一个选择“移动”到另一选择。该示例适用于新页面,但不适用于我的页面。单击后,选定的元素不会移动到其他选择。
问题是还有另一种方法可以做到这一点,或者我如何纠正我的页面以使其正常工作?
示例代码:
<div style="float:left; border:1px solid red;">
<div style="background-color:red; color:white;" align="center">INFORMATIVE</div>
<div id="prop_info" style="float:left;">
<div style="border:1px solid red;">PROPOSTI</div>
<div style="float:left"><select multiple id="sel_info_pro" name="sel_info_pro" class="sel"><option value="b">a</option><option>b</option></select></div>
<div style="float:left;"><a href="#" id="add_info"> >> </a><br><a href="#" id="remove_info"> << </a></div>
</div>
<div id="conf_info" style="float:left;">
<div style="border:1px solid red;">CONFIGURATI</div>
<select multiple id="sel_info_conf" name="sel_info_conf" class="sel"></select>
</div>
</div>
$(document).ready(function(){
$('#add_info').click(function() {
return !$('#sel_info_pro option:selected').remove().appendTo('#sel_info_conf');
});
$('#remove_info').click(function() {
return !$('#sel_info_conf option:selected').remove().appendTo('#sel_info_pro');
});
});
On my page I have to select option values loaded by a $.post function. I have a button to "move" data from one select to the other. The example works in a new page but not in my page not. The elements selected do not move to the other select after the click.
The question is there is another way to do it or how can I correct my page for making it work?
Example code:
<div style="float:left; border:1px solid red;">
<div style="background-color:red; color:white;" align="center">INFORMATIVE</div>
<div id="prop_info" style="float:left;">
<div style="border:1px solid red;">PROPOSTI</div>
<div style="float:left"><select multiple id="sel_info_pro" name="sel_info_pro" class="sel"><option value="b">a</option><option>b</option></select></div>
<div style="float:left;"><a href="#" id="add_info"> >> </a><br><a href="#" id="remove_info"> << </a></div>
</div>
<div id="conf_info" style="float:left;">
<div style="border:1px solid red;">CONFIGURATI</div>
<select multiple id="sel_info_conf" name="sel_info_conf" class="sel"></select>
</div>
</div>
$(document).ready(function(){
$('#add_info').click(function() {
return !$('#sel_info_pro option:selected').remove().appendTo('#sel_info_conf');
});
$('#remove_info').click(function() {
return !$('#sel_info_conf option:selected').remove().appendTo('#sel_info_pro');
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一点指导可能会有所帮助。如果您执行以下操作:
那么无论您选择了多少个,您都会得到 0 所选
选项实际上是针对 jQuery 对象上的
val()
存储的:因此,上述方法将不起作用。您可能必须遍历数组,将它们与列表中的每个项目进行比较才能将它们移动。
此链接也可能有帮助: http://marcgrabanski.com/articles/jquery-select-列表值
A bit guidance that might help. If you did the following:
Then you would get 0, regardless of how many you had selected
The selected options are actually stored against the
val()
on the jQuery object:So, the approach above will not work. You may have to iterate through the array, comparing them to each item in the list to move them over.
This link may also help: http://marcgrabanski.com/articles/jquery-select-list-values
我找到了我的工作解决方案:
});
谢谢大家的帮助!
I found my working solution:
});
thanks everybody for help!