2016-10-03 102 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") 
} 

})