通过将按钮id从Code-behind传递给Jquery Function,Set buttons display no

Set buttons display none property by passing button id from Code behind to Jquery Function?

本文关键字:Function Jquery Set buttons no display 按钮 id Code-behind      更新时间:2024-01-02

我有一个代码隐藏函数,它给了我一个按钮id。现在我想用jQuery隐藏那个特定的按钮。这是后面的代码

if (dt.Rows.Count > 0) {
    for (int i = 2; i < dt.Columns.Count; i++) {
        int status = Convert.ToInt32(dt.Rows[0][i].ToString());
        if (status == 1) {
            string columnname = dt.Columns[i].ToString(); //here column name is button id
            ClientScript.RegisterStartupScript(this.GetType(), "getid", "getid('" + columnname + "');", true);
        }
    }
}

以下是我未能实现的jQuery函数:

$(document).ready(function () {
    function getid(id){
        $(id).toggle('hide');
    } 
});

您可以尝试确保浏览器中的开发人员工具(F12)中没有出现错误(请检查控制台部分)。

此外,由于您的函数是在jQuery"文档就绪"块中定义的,因此无论何时启动脚本触发,它都可能不可用。您可以尝试通过以下方式之一解决此问题:

尝试在jQuery部分之外定义您的函数

function getid(id){
    $(id).toggle('hide');
} 

此外,如果您根本不想依赖jQuery,您可以简单地使用以下代码片段:

function getid(id){
     var e = document.getElementById(id);
     e.style.display = (e.style.display == "block") ? 'none' : 'block';
} 

考虑添加延迟

根据我的经验,我发现RegisterStartupScript方法通常会在您正在调用的内容真正准备好被调用之前启动。您可以通过使用setTimeout()函数在调用本身添加一个轻微的延迟来解决这个问题:

// Set a 10ms delay prior to calling your getid function
ClientScript.RegisterStartupScript(GetType(), "getid", "setTimeout(function(){ getid('" + columnname + "');},10);", true);