2016-10-10 33 views
1

我想用jQuery切換功能來切換div。jQuery切換不起作用在我的頁面

這裏是我的代碼:

$(document).ready(function() { 
 
    $("#removeSongButton").click(function() { 
 
    $("#radioButton1").toggle(); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div> 
 
    <asp:Button runat="server" ID="removeSongButton" Text="remove song" /> 
 
</div> 
 

 
<div id="radioButton1"> 
 
    <asp:RadioButtonList runat="server" ID="radioButton"> 
 
    <asp:ListItem Text="1" Value="1"></asp:ListItem> 
 
    <asp:ListItem Text="2" Value="2"></asp:ListItem> 
 
    <asp:ListItem Text="3" Value="3"></asp:ListItem> 
 
    </asp:RadioButtonList >   
 
</div>

當我點擊 「removeSongButton」 在div TOGע樂了,馬上切換下來。我想可能是頁面重新加載。

+1

[jQuery的翻轉電不工作(http://stackoverflow.com/questions/3708086/jquery-toggle-not-working) –

+0

不告訴ASP來在服務器上運行它 – charlietfl

回答

2

你是對的,它的重裝。 如果按鈕點擊最想做的事情是切換,然後添加像這樣的preventDefault。或者是否有其他要通過按鈕點擊觸發的事件?

$("#removeSongButton").click(function (e) { 
    e.preventDefault(); 
    $("#radioButton1").toggle(); 
}); 
+0

歡呼聲!解決問題,謝謝! –

+0

不客氣 – Fantasterei

-1

嘗試添加div#radioButton1作爲您的選擇器。我創建了一個超級簡單的jsbin http://jsbin.com/rudocofowu/edit?html,output


如果沒有幫助,那麼我將開始與消除toggle(),並嘗試使用剛剛slideUp()slideDown()從jQuery的只是隔離哪一個是搞亂向上。

+0

ID的可能的重複是獨特...將標記名放在前面比僅使用ID的效率低。更改選擇器贏得; t更改行爲OP具有 – charlietfl

+0

@charlietfl它的效率並不低。如果有的話,它更有效率,因爲你正在選擇更具體的東西。你有沒有嘗試過使用.slideUp()或.slideDown()? – James

+0

你不明白dom搜索。一個ID是絕對的,不需要搜索多個節點中的多個節點。該dom跟蹤ID的 – charlietfl