2013-12-19 38 views
0

我有四個菜單選項卡。在一個選項卡中是提交表單(默認),第二個選項卡包含條目列表,每個條目都有一個稱爲更改狀態的按鈕。當我點擊「更改狀態」時,我使用ajax調用windows.reload來刷新頁面,但該選項卡將返回默認的第一個。現在我的問題是我怎麼能更新menutab類到當前選定的菜單成爲一個活躍的。下面是我的阿賈克斯代碼:Ajax更改按鈕提交時鏈接的類名稱

<input type="button" value="change status"> // a button to click to change status 

<a class="active">tab1</a> // need to change classname when the button clicked 
<a class="nonactive">tab2</a> // need to change classname when the button clicked 

<script> 
$(document).ready(function() { 
    $(".closeBTN").click(function() { 

    var data = $(this).attr('id'); 
    //alert (data); 
    $.ajax({ 
     type: "POST", 
     url: "../deals_status_update.php", 
     data: { 'id': data } 
    }).done(function(msg) { 
     $("#notification").html(msg);   
     window.setTimeout(function(){ 
      location.reload() 

      $('#itrade .active').removeClass().addClass('nonactive'); 
      $('#iopendeals .nonactive').removeClass().addClass('active'); 
     },100)   
    }); 
}); 
});   
</script> 
+0

不是'location.reload()'與實際刷新相同嗎?如果是這樣,那麼你只是想捕獲你從ajax調用中得到的任何東西,然後操縱你的DOM結構來顯示你想要的頁面。 - 編輯:'location.reload()'確實刷新整個頁面就像F5一樣:https://developer.mozilla.org/en-US/docs/Web/API/Location.reload – Viridis

回答

0

您正在重新加載頁面,所以後面發生的任何JavaScript都將失去其狀態。 DOM修改僅適用於當前頁面會話。一旦它被導航或重新加載,它們就會失去進展。

你認爲沒有重新加載?爲什麼你要重新加載?

,或者

你能做到,也許這些變化的客戶端,並且而不是重新加載,重定向到不同的GET參數相同的頁面?

<li class="menuitem<?=$_GET['page'] == 'homepage' ? ' active' : ''?>"> 
    Homepage 
</li> 
+0

如果我不會使用reload該頁面需要手動刷新頁面以更新數據。 – user3103905

+0

您已經在AJAX中使用POST,爲什麼不使用GET並動態獲取數據? – casraf

+0

陳您好,我嘗試了獲取函數沒有這個代碼發生: <腳本類型= 「文/ JavaScript的」> \t功能updatestatus() \t { \t VAR ID = document.getElementsByName( 「updateBTN」)的getAttribute。 ('ID'); \t \t \t \t var xmlhttp; \t \t如果(window.XMLHttpRequest){XMLHTTP =新的XMLHttpRequest();} \t \t別的 \t \t { \t \t \t XMLHTTP =新的ActiveXObject( 「Microsoft.XMLHTTP」); \t \t} \t \t xmlhttp.onreadystatechange = function(){if(xmlhttp.readyState == 4 && xmlhttp。狀態== 200) \t \t \t \t { \t \t \t \t \t的document.getElementById( 「通知」)值= xmlhttp.responseText。 \t \t \t \t} \t \t \t} xmlhttp.open( 「GET」, 「update.php ID =?」 + ID,TRUE); \t xmlhttp.send(); \t} – user3103905

1

location.reload()重新加載網站,所以你不能使用jQuery方法。

也許window.location.hash可以幫到你。

添加一個散列並在網站重新加載後解析這個散列來觸發你的類?

1

嘗試使用location.hash,因爲解釋location.reload()將實際刷新頁面,我仍然不明白爲什麼你需要刷新頁面來更新數據!

<input type="button" value="change status"> // a button to click to change status 

<a class="active">tab1</a> // need to change classname when the button clicked 
<a class="nonactive">tab2</a> // need to change classname when the button clicked 

<script> 
$(document).ready(function() { 
    $(".closeBTN").click(function() {  
     var data = $(this).attr('id'); 

     $.ajax({ 
      type: "POST", 
      url: "../deals_status_update.php", 
      data: { 'id': data } 
     }).done(function(msg) { 
      $("#notification").html(msg);   
      window.setTimeout(function(){ 
       location.hash = 'iopendeals'; 
       location.reload(); 
      },100)   
     }); 
    }); 

    if(location.hash != '') { 
     $('.active').removeClass().addClass('nonactive'); 
     $(location.hash + ' .nonactive').removeClass().addClass('active'); 
    } 
});   
</script> 
+0

如果這解決了你的問題,請將它標記爲接受的答案 – MaK