最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamically set an asp.net TextBox tooltip - Stack Overflow

programmeradmin2浏览0评论

I have an asp textbox:

    <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.

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.

Thank You in advance

I have an asp textbox:

    <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.

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.

Thank You in advance

Share Improve this question edited May 20, 2011 at 15:53 Ron Bach asked May 17, 2011 at 18:45 Ron BachRon Bach 331 gold badge2 silver badges7 bronze badges 4
  • Which version are you on? – Ali Habibzadeh Commented May 17, 2011 at 18:49
  • VS 2008 Framework 3.5, sorry should have mentioned this before. – Ron Bach Commented May 18, 2011 at 13:39
  • Are you trying to do this on client-side or server-side? Is your DDL set to autopostback? – gbs Commented May 20, 2011 at 17:10
  • EVERYTHING is client-side, the person who created the site wanted little if any work being done on the server. Where this code will reside, is in the client-side function invoked when the DDL changes. – Ron Bach Commented May 20, 2011 at 18:29
Add a ment  | 

3 Answers 3

Reset to default 2

I tried this like this:

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

And in my jQuery I wrote:

$(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

Something like this using jQuery:

<html xmlns="http://www.w3/1999/xhtml">
<head runat="server">
    <title></title>
     <script type="text/javascript" src="http://ajax.googleapis./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>

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.

发布评论

评论列表(0)

  1. 暂无评论