2013-02-22 52 views
0

虽然它在JSFiddle中工作,但我努力获得一些在我的网站上工作的代码。jquery从一个框移动到另一个

我有这个脚本在身体。

<script type="text/javascript" src="jquery-1.9.1.min.js"></script> 
<script> 
$('.add').click(function(){ 
$('#first option:selected').appendTo('#second'); 
}); 
$('.remove').click(function(){ 
$('#second option:selected').appendTo('#first'); 
}); 
$('.add-all').click(function(){ 
$('#first option').appendTo('#second'); 
}); 
$('.remove-all').click(function(){ 
$('#second option').appendTo('#first'); 
}); 
</script> 

我有这个来执行脚本。

<div id="input14"> 
    <select id="first" multiple="true"> 
     <option value="[email protected]"> test </option> 
     <option value="[email protected]"> [email protected] </option> 
     <option value="[email protected]"> [email protected] </option> 
     <option value="[email protected]"> [email protected] </option> 
     <option value="[email protected]"> [email protected] </option> 
    </select> 
    </div> 

    <div id="button14"> 



     <center> 
     <br /> 
     <button class='add'> > </button><br /> 
     <button class='remove'> < </button><br /> 
     <button class='add-all'> >>> </button><br /> 
     <button class='remove-all'> <<< </button> 
     </center> 
    </div> 

    <div id="error14"> 

     <select id="second" multiple="true"> 

     </select> 



    </div> 

正如我说,它工作在的jsfiddle但不是在我的本地,这里是设置的jsfiddle http://jsfiddle.net/8nezD/1/

请有人可以告诉我的我的方式错误!

感谢

回答

1

包装你的jQuery的文件准备呼叫

$(document).ready(function() { 
    $('.add').click(function() { 
     $('#first option:selected').appendTo('#second'); 
    }); 
    $('.remove').click(function() { 
     $('#second option:selected').appendTo('#first'); 
    }); 
    $('.add-all').click(function() { 
     $('#first option').appendTo('#second'); 
    }); 
    $('.remove-all').click(function() { 
     $('#second option').appendTo('#first'); 
    }); 
}); 

它的工作原理上的jsfiddle是因为该网站是自动包装为您的原因。

+0

我做了这个,但它没有工作? – brew84 2013-02-22 18:02:50

+0

你看过你的开发者控制台是否有错误? – j08691 2013-02-22 18:03:14

+0

brew84 2013-02-22 18:03:22

相关问题