简体   繁体   English

使用Jquery或Javascript清除OnFocus上的Asp.Net TextBox控件

[英]Clear Asp.Net TextBox Control on OnFocus with Jquery or Javascript

I have been scouring the interwebs and stackoverflow, but I have not come across a solution that works. 我一直在寻找互连网和stackoverflow,但是我没有遇到一个可行的解决方案。 I need to erase the default text from the textbox when onfocus happens. 发生对焦时,我需要从文本框中删除默认文本。 I know it is hitting onfocus because my class for text color is set in there also and the color is changing. 我知道它正在引起人们的关注,因为我的文本颜色类也在其中设置并且颜色正在变化。 Here's my code and what I have tried. 这是我的代码以及我尝试过的内容。

ASP: ASP:

<asp:TextBox ID="txtAddEditDel" runat="server" CssClass="txtAddEditDel" ></asp:TextBox>

The default text is some random instructions it populates on Page_Load. 默认文本是填充在Page_Load上的一些随机指令。

Jquery: jQuery的:

$(".txtAddEditDel").focus(function () {
             var selectedRoleText = $(this).find('option:selected').text();
             var selectedRoleId = $(this).find('option:selected').val();
             var selectedRoleIndex = $(this).find('option:selected').index();

            //enable add button
            enableDisableLinkButtons(true, true, true);

            if (selectedRoleText == 'Create, Edit, or Delete a Role Name.') {
                $(".txtAddEditDel:text").val('');
            }
            $(".txtAddEditDel").removeClass('placeholderText');

        });

The removeClass function is hit, but my value is not cleared. removeClass函数被命中,但我的值未清除。 Heres what I have tried: 这是我尝试过的:

 $(".txtAddEditDel").val('');
 $(".txtAddEditDel").val("");
 $(".txtAddEditDel").text("");
 $(".txtAddEditDel").text == '';
 $(".txtAddEditDel").text == "";
 $(this).val('');
 document.getElementbyId('<%= txtAddEditDel.ClientID %'>.value = "";

$(".txtAddEditDel").val(''); $( “txtAddEditDel”)VAL( '')。 should work, assuming its getting there. 应该工作,假设它到达那里。 Have you put some debugging information to check the if clause is true? 您是否放置了一些调试信息来检查if子句是否正确? It may be a better idea to check against the value of the option rather than the text, as the text could easily be misspelt or changed where as a default value will, in my experience, be '' or 0 in this scenario 检查选项的值而不是文本是一个更好的主意,因为文本很容易被误拼或更改,以我的经验,在这种情况下默认值将为”或0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM