在jquery中设置DIV标签可见性

本文关键字:标签 可见性 DIV 设置 jquery | 更新日期: 2023-09-27 17:49:28

我的jQuery代码是

$.ajax({
        type: 'POST',
        url: "~/Pages/test.aspx",
        data: "json",
        success: function (response) {
            $('#testSpan').html(response.HasCases);
    },
        error: function (e1, e2, e3) {
            $('#testSpan').html('Error');
    }
});

我得到的响应值为True或False。如果我的值为True,我应该显示DIV标签值,否则我应该隐藏DIV标签。上面的代码在div文本的位置显示为真或假值:(.

在jquery中设置DIV标签可见性

根据布尔值显示或隐藏元素

 $('#testSpan').toggle(response.HasCases);
参考:

切换()

你可以使用jQuery toggle()来实现这一点:

if(response.HasCases == "true")
   $('#testSpan').toggle();

您需要使用show()hide()代替html()

if(response.HasCases == "true")
   $('#testSpan').show();
else 
  $('#testSpan').hide();

你的代码应该是

$.ajax({
        type: 'POST',
        url: "~/Pages/test.aspx",
        data: "json",
        success: function (response) {       
          if(response.HasCases == "true")
             $('#testSpan').show();  
        }, error: function (e1, e2, e3){   
            $('#testSpan').show();           
        }
});