c# - How to clear a textbox on single press of Backspace key using Javascript? - Stack Overflow

I m working on a web application that contains a text box. The value of text box is filled from Date pi

I m working on a web application that contains a text box. The value of text box is filled from Date picker and its property is read only by default. till now everything is working fine. But problem occurs when I press backspace button. Rather than clearing the text box it is redirecting me back on the previous page.

i handled this situation by a javascript function by referring some previous answers on this site.

 function allowBackSpace(val) {
    var keyCodeEntered = (event.which) ? 
                             event.which : 
                             (window.event.keyCode) ? 
                                 window.event.keyCode : 
                                 -1;
    if (keyCodeEntered == 8) {
        $(this).val(""); 
        return true;
    }
    return false;
   }

UPDATE : this function is called from

<asp:TextBox ID="txtDate" 
             Style="margin-left: 5px; margin-right: 5px" 
             runat="server"
             Width="88px" 
             onkeypress="return allowBackSpace(this);">
</asp:TextBox>

I m calling this function on key press event of the textbox. The preoblem is that it is allowing only to delete one character at a time.

I wish to clear the whole textbox if user presses backspace button. Stuck here as how to achieve this.

Thanks in advance Akhil

PS:Any way to achieve this using C# code will also be very helpful.

I m working on a web application that contains a text box. The value of text box is filled from Date picker and its property is read only by default. till now everything is working fine. But problem occurs when I press backspace button. Rather than clearing the text box it is redirecting me back on the previous page.

i handled this situation by a javascript function by referring some previous answers on this site.

 function allowBackSpace(val) {
    var keyCodeEntered = (event.which) ? 
                             event.which : 
                             (window.event.keyCode) ? 
                                 window.event.keyCode : 
                                 -1;
    if (keyCodeEntered == 8) {
        $(this).val(""); 
        return true;
    }
    return false;
   }

UPDATE : this function is called from

<asp:TextBox ID="txtDate" 
             Style="margin-left: 5px; margin-right: 5px" 
             runat="server"
             Width="88px" 
             onkeypress="return allowBackSpace(this);">
</asp:TextBox>

I m calling this function on key press event of the textbox. The preoblem is that it is allowing only to delete one character at a time.

I wish to clear the whole textbox if user presses backspace button. Stuck here as how to achieve this.

Thanks in advance Akhil

PS:Any way to achieve this using C# code will also be very helpful.

Share Improve this question edited Jul 30, 2012 at 7:39 Kjartan 19.2k16 gold badges75 silver badges100 bronze badges asked Jul 30, 2012 at 6:38 akhilakhil 1,2223 gold badges22 silver badges43 bronze badges 4
  • 1 can you post some explanation of this code? For instance, what is the argument val? – Jibi Abraham Commented Jul 30, 2012 at 6:44
  • 1 Have you tried giving the focus back to that textbox ? – JohnnBlade Commented Jul 30, 2012 at 6:45
  • 1 sometimes backspace event is depend on browser patibility..which browser you are using?? – user1102001 Commented Jul 30, 2012 at 6:46
  • @JohnnBlade No I didnt tried that. Can you explain this with some code? – akhil Commented Jul 30, 2012 at 6:52
Add a ment  | 

4 Answers 4

Reset to default 2

You are returning true from js function, which also performs its keypress event. So, try with returning false. You can try the following code:

function allowBackSpace(val) {

var keyCodeEntered = 
     (event.which) ? event.which : 
                 (window.event.keyCode) ? window.event.keyCode : -1;
if (keyCodeEntered == 8) 
{
     $(this).val("");
     return false;
 }
 return false; }

Try this function.

 $("#txt1").keydown(function(eve){
    var keyCodeEntered = eve.keyCode? eve.keyCode : eve.charCode;

    if (keyCodeEntered == 8) 
    {
         $(this).val("");
         return false;
     }
     return false;
  });
});

Use Jquery

$("#txtDate").keyup(function(event){
    if(event.keyCode == 8){
        $("#txtDate").val("");
        return false;
    }
});
function allowBackSpace(e,obj)
{
    var evt=e||window.event;
    if(evt.keyCode==8)
    {
        obj.value='';
    }
}

Now, use onkeydown event instead of onkeypress

<textarea onkeydown='allowBackSpace(event,this)'></textarea>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744763261a4592299.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信