我希望在完成转换后,在左右两个方框中都获取值。这是html代码
<section class="container">
<div>
<select id="leftValues" name="leftValues[]" size="5" multiple>
<option value="a">1</option>
<option value="b">2</option>
<option value="c">3</option>
</select>
</div>
<div>
<input type="button" id="btnLeft" value="<<" />
<input type="button" id="btnRight" value=">>" />
</div>
<div>
<select id="rightValues" name=rightValues[] size="4" multiple>
<option value="x">9</option>
<option value="y">8</option>
<option value="z">7</option>
</select>
<div>
<input type="text" id="txtRight" />
</div>
</div>
</section>
<button type="submit" class="blue">Save</button>
这是我正在使用的脚本
<script>
$("#btnLeft").click(function () {
var selectedItem = $("#rightValues option:selected");
$("#leftValues").append(selectedItem);
});
$("#btnRight").click(function () {
var selectedItem = $("#leftValues option:selected");
$("#rightValues").append(selectedItem);
});
$("#rightValues").change(function () {
var selectedItem = $("#rightValues option:selected");
$("#txtRight").val(selectedItem.text());
});
</script>
这是CSS
<style>
SELECT, INPUT[type="text"] {
width: 160px;
box-sizing: border-box;
}
SECTION {
padding: 8px;
background-color: #f0f0f0;
overflow: auto;
}
SECTION > DIV {
float: left;
padding: 4px;
}
SECTION > DIV + DIV {
width: 40px;
text-align: center;
}
</style>
完成所有交换并单击“保存”按钮后,我希望将所有新值交换到左侧,将新值交换到右侧。
答案 0 :(得分:0)
试试这个.. http://jsfiddle.net/qfefW/2/
在jsFiddle中,我进行了html修改以包含类名,并修改了默认方法,以便在从左到右选择时更改类名。
var left;
var right;
var newLeft;
var newRight;
var addedLeftValues = new array();
var addedRightValues = new array();
$(document).ready(function() {
SaveOldValues();
});
function SaveOldValues()
{
left = new Array();
right = new Array();
$('#leftValues .left').each(function(){
left.push($(this).val());
});
$('#rightValues .right').each(function(){
right.push($(this).val());
});
}
function UpdatedValues()
{
newLeft = new Array();
newRight = new Array();
$('#leftValues .left').each(function(){
newLeft.push($(this).val());
});
$('#rightValues .right').each(function(){
newRight.push($(this).val());
});;
}
$('.blue').on("click",function(){
UpdatedValues();
$(newLeft).each(function(k,value){
if($.inArray(value, left) == -1)
{
alert("new value in left is: " + value);
addedLeftValues.push(value);
}
});
$(newRight).each(function(k,value){
if($.inArray(value, right) == -1)
{
alert("new value in Right is: " + value);
addedRightValues.push(value);
}
});
});
function SendValuesToPage()
{
var leftData = JSON.Stringify(addedLeftValues);
var rightData = JSON.Stringify(addedRightValues);
// your ajax code
}