当用户输入新数字或字符并开始输入javascript或jquery时如何删除先前的文本 [英] how to remove previous text when user enter a new number or character and start typing in javascript or jquery

查看:67
本文介绍了当用户输入新数字或字符并开始输入javascript或jquery时如何删除先前的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,我希望每当用户开始在该texbox中输入数字时,都应删除以前的值.

假设有一个包含数字23的文本框,我希望每当用户开始在同一文本框23中输入内容时,都应删除它并显示新的输入数字.

我的代码如下

I have a textbox ,i want whenever user start entering a number in that texbox the previous value should be remove.

Let say there is a textbox which contain a number as 23 ,i want whenever user start typing in that same textbox 23 shold be remove and new enetered number should be shown.

My code as follow

document.getElementById("mytextbox").select();
$("#mytextbox").select();

<input type="text" onclick="clear();">



但是我的代码仍然无法正常工作,我使用的是ie9,窗口7是64位PC,我调试了代码,并对这行做了快速监视



But still my code is not working,i am using ie9 ,window 7 64 bit pc,i debug the code and did quickwatch to this line

document.getElementById("mytextbox").select()

它显示给我未定义.,我能够找到mytextbox.
还有其他方法可以执行此操作.我的代码在chrome和mozilla中运行.

it is showing me undefined.,I am able to find the mytextbox.
Is there any other way to do this.My code is working in chrome and mozilla.

推荐答案

(#mytextbox").select(); < input type ="text" onclick ="clear();">
("#mytextbox").select(); <input type="text" onclick="clear();">



但是我的代码仍然无法正常工作,我使用的是ie9,窗口7是64位PC,我调试了代码,并对这行做了快速监视



But still my code is not working,i am using ie9 ,window 7 64 bit pc,i debug the code and did quickwatch to this line

document.getElementById("mytextbox").select()

它显示给我未定义.,我能够找到mytextbox.
还有其他方法可以执行.我的代码在chrome和mozilla中运行.

it is showing me undefined.,I am able to find the mytextbox.
Is there any other way to do this.My code is working in chrome and mozilla.


首先,如果您已经在使用jQuery,则不需要document.getElementById.使用jQuery 选择器:
http://api.jquery.com/category/selectors/ [input元素.对于所有输入元素,可通过其属性value:
访问其值
http://www.w3schools.com/tags/tag_input.asp [ http://www.w3schools.com/tags/att_input_value.asp [ http://www.w3schools.com/jsref/prop_option_value.asp [
To start with, if you are already using jQuery, document.getElementById is not needed, as you can use jQuery selectors:
http://api.jquery.com/category/selectors/[^].

Now the solution: text box is actually the input element with the attribute type="text". For all input elements, its value is accessed through its property value:
http://www.w3schools.com/tags/tag_input.asp[^],
http://www.w3schools.com/tags/att_input_value.asp[^],
http://www.w3schools.com/jsref/prop_option_value.asp[^].

So, apparently, you only need to assign this value to empty string, in the handler of the event you need under condition you need.

However, I would not advise you to do it. Usually, the user knows better what to delete from input data and when. By the user will be only frustrated if the input data is cleaned without her/his order. Maybe the user wants to repeat the same value or modify just one or too characters in it? If I, personally, came across the behavior you describe, I would do all my best to avoid your site. Some of your users might feel the same. Do you need it?

—SA


尝试正常运行:-)
Try this its working fine :-)
<script type="text/javascript">


这篇关于当用户输入新数字或字符并开始输入javascript或jquery时如何删除先前的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆