2017-02-06 19 views
0

我是JQuery的新手。我想在我的腳本中添加條件語句,但我不確定按鈕效果的語法。我wan't它沿jQuery中的按鈕狀態的條件語句?

線,如果「.save」 = .show然後.hide「.donedit」

這個腳本是表,我不希望用戶能夠如果他們編輯了一個字段而不保存內容,點擊.donedit按鈕。

現在.save按鈕僅在用戶編輯字段時才顯示,如果顯示.save按鈕,理想的功能是灰顯或隱藏donedit按鈕。

這裏的相關部分:

$(document).ready(function(){ 

     //individual field edit buttons show as hidden  
     $(".edit").hide(); 
     $(".donedit").hide(); 


     //when the mass edit button is clicked in the header, the edit button 
     //will show for each field, the massedit button will hide showing the donedit button 

     $(".massedit").click(function(){ 
      $(".edit").show(); 
      $(".massedit").hide(); 
      $(".donedit").show(); 
     }); 

     //When the donedit button is clicked the massedit button shows and the 
     // donedit button disappears 

     $(".donedit").click(function(){ 
      $(".edit").hide(); 
      $(".massedit").show(); 
      $(".donedit").hide();    
     }); 

    }); 
+4

表現出一定的HTML兄弟 –

回答

0

如果您使用的顯示和隱藏,你可以做

$(".save").css("display") 

如果返回「塊」,這意味着目前正在顯示它,如果它返回「無」,那麼它目前隱藏。所以,你可以檢查:

if ($(".save").css("display") != "none") { 
    //donedit button code can run 
} 

這不是一個偉大的方式做到這一點,因爲你所要求的國家的DOM,而不是僅僅存儲與否「.save」被隱藏,但這可能是最簡單的解決方案。

編輯:

你甚至可以在else語句拋出,如果你想和提醒他們正試圖關閉編輯不保存,或使用確認用戶如果返回true,則讓他們關閉而不保存

或更好,但只是改變代碼

if ($(".save").css("display") != "none" && confirm("Continue without saving")) { 
    //donedit button code can run 
}