【问题标题】:hide and show in javascript在javascript中隐藏和显示
【发布时间】:2023-04-02 10:26:01
【问题描述】:

我有一个想要隐藏的对象。这是我网站上数据网格中的一行。网格中的数据是动态的。当我单击我在下拉列表中选择的“计算机”时,我希望该行隐藏/不可见。我想我必须使用 getElementById()。

这是我要隐藏的id

<span id="dg_form_ctl05_lbl_show_tag" style="display:inline-block;background-color:Transparent;border-color:Navy;border-width:3px;border-style:Double;font-family:Arial;font-size:12px;width:130px;">Subject*</span>    

这是下拉列表 ID。 dg_form_ctl02_DropDownList1

这是我目前拥有的代码,但它似乎不正确,因为当我运行它时它没有隐藏行。

function hideMe() {
var g = document.getElementById("dg_form_ctl05_lbl_show_tag");
var e = document.getElementById("dg_form_ctl02_DropDownList1");
if(e == "Computer") 
g.style.display = 'none';
}

我认为我需要在此使用代码隐藏,这也是我迄今为止为 c# 编写的代码。

  if (!ClientScript.IsStartupScriptRegistered("hwa"))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "hwa", "hideMe();", true);
        }

有人可以帮我吗?

【问题讨论】:

  • if(e == "Computer") 是你出错的地方。 getElementById 函数找到的对象永远不会等于字符串
  • 哦,好吧,我在 e = document.getElementById("dg_form_ctl02_DropDownList1"); 中添加了一个 .value;
  • 深思熟虑:编写问题所花费的时间比即使是最基本的调试测试也需要更长的时间。条件中的一个简单警告将表明它永远不会评估为真,这会导致您检查条件中的值,这应该会导致明显的“duh”时刻意识到您正在将 DOM 元素与字符串进行比较。

标签: javascript jquery asp.net gridview code-behind


【解决方案1】:
function hideMe() {
    var g = document.getElementById("dg_form_ctl05_lbl_show_tag");
    var e = document.getElementById("dg_form_ctl02_DropDownList1").value;
    if(e == "Computer") 
    g.style.display = 'none';
}

【讨论】:

    【解决方案2】:

    您应该使用value 属性来获取选择元素的值,目前您正在将stringobject 进行比较,试试这个:

    var e = document.getElementById("dg_form_ctl02_DropDownList1").value;
    

    【讨论】:

    • 好的,我以前有过。谢谢。
    猜你喜欢
    • 2017-09-03
    • 2017-02-11
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    相关资源
    最近更新 更多