2013-02-22 53 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

相關問題