【问题标题】:Set DIV tag visibility in jquery在 jquery 中设置 DIV 标记可见性
【发布时间】:2012-12-26 11:11:49
【问题描述】:

我的 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 文本的位置显示为 true 或 false 值:( .

【问题讨论】:

    标签: c# jquery json.net


    【解决方案1】:

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

     $('#testSpan').toggle(response.HasCases);
    

    参考:

    toggle()

    【讨论】:

      【解决方案2】:

      您可以使用 jQuery toggle() 来实现:

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

      【讨论】:

        【解决方案3】:

        你需要使用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();           
                }
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-09-15
          • 2017-10-31
          • 1970-01-01
          • 2019-10-06
          • 2014-07-04
          • 2011-07-28
          • 2011-04-19
          • 2014-02-11
          相关资源
          最近更新 更多