gpt4 book ai didi

Jquery - 根据组合框选择将项目从一个列表移动到另一个列表

转载 作者:行者123 更新时间:2023-12-01 06:06:15 25 4
gpt4 key购买 nike

更新:- 我更新的脚本是第二个,并且运行完美(我自己找到了解决方案),但我不喜欢它的编写方式。任何东西都可以改变以使其看起来更好吗?

下面是我编写的完整代码。它的作用是

  1. 基于 filterDis 选择框,它填充 sourceCars 选择框
  2. 当用户双击 sourceCars 时,会将汽车移动到 targetCars 选择框
  3. 当用户双击 targetCars 时,会将汽车移动到源汽车并对源进行排序

这看起来不像是一个理想的解决方案,并且存在一些问题:

  1. filterDis选择框中选择GM,双击任何项目并将其移动到目标。从过滤器中选择“全部”,它会删除目标中的所有内容并填充源汽车列表中的所有内容。 即使我选择“全部”,有什么办法可以保留目标汽车并仅显示源列表中的其他汽车?如果我首先选择“GM”并移动某些东西,这也会出现问题定位并选择其他汽车类型,然后再次选择“GM”,这会将汽车从目标中删除...
  2. 如果我选择“GM”并将一辆汽车移动到目标,然后选择“Honda”并双击我们选择的目标 GM 汽车..这将添加到源 Honda 列表中..

我不确定我在源端所做的排序是否是正确的解决方案..

有什么捷径可以实现这一点吗?

感谢您的帮助

问候

基兰

下面是完整代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

<style type="text/css">
body
{padding:0; margin:0; font-weight:normal;font-size:13px;}

div#townDiv {
width :100px;
}
select.car{
margin:30px;
width:220px;
}
</style>


<script language="javascript" type="text/javascript">
$(function() {
var sourceCars=$('#sourceCars option').clone();
$('#filterDis').change(function() {
var val = $(this).val();
$('#sourceCars').empty();
sourceCars.filter(function(idx, el) {
return val === 'ALL' || $(el).text().indexOf('[' + val + ']') >= 0;
}).appendTo('#sourceCars');
});

$('#sourceCars').dblclick(function() {
$('#sourceCars option:selected').appendTo('#targetCars');

});

$('#targetCars').dblclick(function() {
$('#targetCars option:selected').appendTo('#sourceCars');
var foption = $('#sourceCars option:first');
var soptions = $.makeArray($('#sourceCars option:not(:first)')).sort(function(a, b) {
return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
});
$('#sourceCars').html(soptions).prepend(foption);
foption.attr("selected", true).siblings("option").removeAttr("selected");
});
});

</script>
<link href="styles.css" type="text/css" rel="Stylesheet" />
<title></title>
</head>
<body>


<div id="townDiv">
<select id="filterDis" class="car">
<option selected="true" >ALL</option>
<option>GM</option>
<option>Honda</option>
<option>Ford</option>
</select>

<select id="sourceCars" class="car" size="20" >
<option>Chevy [GM]</option>
<option>Buick [GM]</option>
<option>Civic [Honda]</option>
<option>Accord [Honda]</option>
<option>Focus [Ford]</option>
</select>

<select id="targetCars" class="car" size="20" >
</select>

</div>
</body>
</html>

更新得到了答案,就在这里

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

<style type="text/css">
body
{padding:0; margin:0; font-weight:normal;font-size:13px;}

div#townDiv {
width :100px;
}
select.car{
margin:30px;
width:220px;
}
</style>


<script language="javascript" type="text/javascript">
$(function() {
var sourceCars=$('#sourceCars option').clone();
$('#filterDis').change(function() {
var val = $(this).val();
$('#sourceCars').empty();
sourceCars.filter(function(idx, el) {
var found=false;
$('#targetCars option').each(function(){
if ($(this).val()===$(el).text())
found=true;
});

if(found)
return false;

return val === 'ALL' || $(el).text().indexOf('[' + val + ']') >= 0;
}).appendTo('#sourceCars');
});

$('#sourceCars').dblclick(function() {
$('#sourceCars option:selected').appendTo('#targetCars');
});

$('#targetCars').dblclick(function() {
var targetList=$('#targetCars option:selected');
var filterVal= $('#filterDis').val();
if( filterVal === 'ALL' || targetList.text().indexOf('[' + filterVal + ']') >= 0)
targetList.appendTo('#sourceCars');
else
targetList.remove();
var foption = $('#sourceCars option:first');
var soptions = $.makeArray($('#sourceCars option:not(:first)')).sort(function(a, b) {
return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
});
$('#sourceCars').html(soptions).prepend(foption);
foption.attr("selected", true).siblings("option").removeAttr("selected");
});
});

</script>
<link href="styles.css" type="text/css" rel="Stylesheet" />
<title></title>
</head>
<body>


<div id="townDiv">
<select id="filterDis" class="car">
<option selected="true" >ALL</option>
<option>GM</option>
<option>Honda</option>
<option>Ford</option>
</select>

<select id="sourceCars" class="car" size="20" >
<option>Chevy [GM]</option>
<option>Buick [GM]</option>
<option>Civic [Honda]</option>
<option>Accord [Honda]</option>
<option>Focus [Ford]</option>
</select>

<select id="targetCars" class="car" size="20" >
</select>

</div>
</body>
</html>

最佳答案

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

<style type="text/css">
body
{padding:0; margin:0; font-weight:normal;font-size:13px;}

div#townDiv {
width :100px;
}
select.car{
margin:30px;
width:220px;
}
</style>


<script language="javascript" type="text/javascript">
$(function() {
var sourceCars=$('#sourceCars option').clone();
$('#filterDis').change(function() {
var val = $(this).val();
$('#sourceCars').empty();
sourceCars.filter(function(idx, el) {
var found=false;
$('#targetCars option').each(function(){
if ($(this).val()===$(el).text())
found=true;
});

if(found)
return false;

return val === 'ALL' || $(el).text().indexOf('[' + val + ']') >= 0;
}).appendTo('#sourceCars');
});

$('#sourceCars').dblclick(function() {
$('#sourceCars option:selected').appendTo('#targetCars');
});

$('#targetCars').dblclick(function() {
var targetList=$('#targetCars option:selected');
var filterVal= $('#filterDis').val();
if( filterVal === 'ALL' || targetList.text().indexOf('[' + filterVal + ']') >= 0)
targetList.appendTo('#sourceCars');
else
targetList.remove();
var foption = $('#sourceCars option:first');
var soptions = $.makeArray($('#sourceCars option:not(:first)')).sort(function(a, b) {
return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
});
$('#sourceCars').html(soptions).prepend(foption);
foption.attr("selected", true).siblings("option").removeAttr("selected");
});
});

</script>
<link href="styles.css" type="text/css" rel="Stylesheet" />
<title></title>
</head>
<body>


<div id="townDiv">
<select id="filterDis" class="car">
<option selected="true" >ALL</option>
<option>GM</option>
<option>Honda</option>
<option>Ford</option>
</select>

<select id="sourceCars" class="car" size="20" >
<option>Chevy [GM]</option>
<option>Buick [GM]</option>
<option>Civic [Honda]</option>
<option>Accord [Honda]</option>
<option>Focus [Ford]</option>
</select>

<select id="targetCars" class="car" size="20" >
</select>

</div>
</body>
</html>

关于Jquery - 根据组合框选择将项目从一个列表移动到另一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5756703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com