javascript 动态设置一个asp.net TextBox 工具提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6035602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:14:30  来源:igfitidea点击:

Dynamically set an asp.net TextBox tooltip

javascriptjqueryasp.nettooltip

提问by Ron Bach

I have an asp.net textbox:

我有一个 asp.net 文本框:

    <asp:TextBox runat="server" id="tb" ToolTip="A" />.

That I need to be able to set the tooltip based on a value selected from a dropdown list.

我需要能够根据从下拉列表中选择的值来设置工具提示。

    If (var == "something") {  
        ToolTip = "B";  
    } else {  
        ToolTip = "A";  
    }

I'm setting the ToolTip to an inital value based on what the DropDown defaults to. When it changes I need to set the ToolTip.

我将 ToolTip 设置为基于 DropDown 默认值的初始值。当它发生变化时,我需要设置工具提示。

I have seen and tried several solutions from the web, but have been unable to make them work.

我已经从网上看到并尝试了几种解决方案,但一直无法使它们起作用。

I'm using and VS 2008 and IE7, the site is running using the VS server and not IIs. I have verified that I have the correct field and CAN change the 'title' using the debugger.

我正在使用 VS 2008 和 IE7,该站点正在使用 VS 服务器而不是 II 运行。我已经验证我有正确的字段,并且可以使用调试器更改“标题”。

Thank You in advance

先感谢您

回答by Ali Habibzadeh

I tried this like this:

我这样试过:

<asp:TextBox runat="server" ID='txtSomething' ToolTip='Some tooltip' CssClass='myTextBox'></asp:TextBox>

And in my jQuery I wrote:

在我的 jQuery 中,我写道:

$(function () {

        var maybe = true;

        if (maybe) {
            $('.myTextBox').attr('title', 'Some other tooltip');
        }

    });

And my text box when rendered shows 'Some other tooltip' as its title

渲染时我的文本框显示“其他工具提示”作为其标题

回答by gbs

Something like this using jQuery:

使用 jQuery 是这样的:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#<%=DropDownList1.ClientID %>").change(function () {
                var tooltip = "B";
                var val = $(this).val();
                if (val == "One") {
                    tooltip = "A";
                }
                $("#<%=TextBox1.ClientID %>").attr('title', tooltip);
            });
        });
</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Text="One" />
        <asp:ListItem Text="Two" />
        <asp:ListItem Text="Three" />
    </asp:DropDownList>
    <asp:TextBox ID="TextBox1" runat="server" ToolTip="A"></asp:TextBox>
    </form>
</body>
</html>

Update - class selector way:

更新 - 类选择器方式:

$(function () {
            $('.myddl').change(function () {
                var tooltip = "B";
                var val = $(this).val();
                if (val == "One") {
                    tooltip = "A";
                }
                $('.mytb').attr('title', tooltip);
            });
        });

 <asp:DropDownList ID="DropDownList1" runat="server" CssClass="myddl">

 <asp:TextBox ID="TextBox1" runat="server" ToolTip="A" CssClass="mytb"></asp:TextBox>

回答by Ron Bach

The code I have listed in my question works and does change ToolTips. The problem lies with the person who created the site and his machinations of CSS and Javascript/JQuery/Json; somehow the ability to access ToolTips was disabled.

我在问题中列出的代码有效并且确实更改了工具提示。问题在于创建站点的人以及他对 CSS 和 Javascript/JQuery/Json 的诡计;以某种方式禁用了访问工具提示的能力。