2016-10-03 98 views
-3

當前如果查詢類型在CRM中有公告,則顯示該查詢類型,但是如果查詢類型沒有公告類型,我希望顯示的公告隱藏起來,但我不確定如何執行此操作,我認爲它可能會成爲別人。請指教。在JavaScript中隱藏div類?

$('#enquirytype').on('click', function (e) { //onlick of professions DD 
     var selectedVal = $(this).val(); //Variable selectedVal this .value 
     $.ajax({ 
      type: 'GET', //Get 
      url: "@Url.Action("GetEnquiryTypeAndAnnoncements", "UnauthEnquiry")", //It goes to the enquiry controller method GetenquiryTypes 
      dataType: 'json', //Datatypes JSON 
      data: { SelectedEnquiryType: selectedVal }, //data is SelectedProfession: selectedVal 
      success: function (json) { //Jquery Parse Json data from server on ajax success 

       if (json.helptext != undefined && json.helptext != '') { 
        $('#enquiryTypeHelp').html(json.helptext) 
        $('#enquiryTypeHelpAlert').show(); /////// 
       } 
       else 
        $('#enquiryTypeHelpAlert').hide(); /////// 


var announcement = $('.EnquiryTypeAnnouncement:first').clone(); 
$('#EnquiryTypeAnnouncements').empty(); 

$('#enquiryTypeHelp').html(json.helptext); 
for (var i in json.announcements) { 
    var announcementCopy = announcement.clone(); 
    announcementCopy.find(".notification").html(json.announcements[i]); 
    $(announcementCopy).appendTo($('#EnquiryTypeAnnouncements')).show(); 
    $('#EnquiryTypeAnnouncements').show(); 
} 
+0

和你有測試此?它工作嗎?如果不是,問題究竟是什麼? – ADyson

回答

0

我不明白,如果你正在尋找代碼優化,或者如果你得到一個錯誤?

在任何情況下

首先你缺少括號內爲封閉:

$('#enquirytype').on('click', function (e) { //onlick of professions DD 
     var selectedVal = $(this).val(); //Variable selectedVal this .value 
     $.ajax({ 
      type: 'GET', //Get 
      url: "@Url.Action("GetEnquiryTypeAndAnnoncements", "UnauthEnquiry")", //It goes to the enquiry controller method GetenquiryTypes 
      dataType: 'json', //Datatypes JSON 
      data: { SelectedEnquiryType: selectedVal }, //data is SelectedProfession: selectedVal 
      success: function (json) { //Jquery Parse Json data from server on ajax success 

       if (json.helptext != undefined && json.helptext != '') { 
        $('#enquiryTypeHelp').html(json.helptext) 
        $('#enquiryTypeHelpAlert').show(); /////// 
       } 
       else 
        $('#enquiryTypeHelpAlert').hide(); /////// 

      } 



    var announcement = $('.EnquiryTypeAnnouncement:first').clone(); 
    $('#EnquiryTypeAnnouncements').empty(); 

    $('#enquiryTypeHelp').html(json.helptext); 
    for (var i in json.announcements) { 
     var announcementCopy = announcement.clone(); 
     announcementCopy.find(".notification").html(json.announcements[i]); 
     $(announcementCopy).appendTo($('#EnquiryTypeAnnouncements')).show(); 
     $('#EnquiryTypeAnnouncements').show(); 
    } 

} 
})); // added this 

我會建議顯示和隱藏添加和移除類的最佳實踐:

$(function(){ 
    if(condition){ 
     $(target).addClass("hidden") 
    } 

    else{ 
     $(target).removeClass("hidden") 
} 

})