【问题标题】:Checking asp.net label properites with jquery function使用 jquery 函数检查 asp.net 标签属性
【发布时间】:2015-09-07 11:58:15
【问题描述】:

我有一个简单的 asp.net 标签,它绑定在 Page_Load() 函数后面的代码中。

<asp:Label runat="server" ID="text"></asp:Label>

我编写了一个 jquery 函数来检查标签是否为空。此函数在asp:button onclientclick 上触发。

function confirmationAccept() {
    var g = $("#<%=text.ClientID%>").val();
    if (g.length > 0) {
        confirm("Accept?");
    }
    else
    {
        confirm("or not?");
        return false;
    }
}

但它不起作用。与var g 对齐似乎是错误的。

【问题讨论】:

    标签: c# jquery asp.net webforms


    【解决方案1】:

    标签没有.val(),需要使用.text().html()

    var g = $("#<%=text.ClientID%>").text();
    

    还要确保在使用之前添加 jQuery。

    【讨论】:

    • 我试试这个,但确认框仍然没有被触发。
    • 你能告诉我们确认按钮的代码和生成的html吗?
    • 但我仍然有长度属性的问题,它总是返回 0
    • 你没有标签文本,所以长度为 0, 会给你 3
    • 我在页面加载方法中绑定了这个标签,但我也像你一样尝试,它仍然不起作用。
    【解决方案2】:

    您好,请在 jquery 函数中使用 html() 和 val()。

    function confirmationAccept(){
    var g = $("#<%=text.ClientID%>").html();
    if(g.length>0){
    confirm("Accept?");
    }else{
    confirm("or not?");
    return false;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-20
      • 2013-08-26
      • 2011-12-02
      • 2018-08-01
      相关资源
      最近更新 更多