简体   繁体   English

非常简单的JavaScript来删除文本框中的值

[英]really simple javascript to remove value in text box

iv got a really simple javascript question. iv有一个非常简单的javascript问题。 Ill be using query for parts of it here but there are akin ways of doing it via javascript. 这里我会使用查询来查询部分内容,但是有一些通过javascript进行查询的方式。 basically, I'm writing a little script that makes it so when you click a text box with a value in it, it will take out the value so your can type (ex for most username boxes they have a little note in there). 基本上,我正在写一个小脚本,因此单击一个文本框中的值时,它将带走该值,以便您可以键入(例如,对于大多数用户名框,它们都有一个小注释)。 there are probably much better ways of doing this (i can already think of some) so feel free to suggest them as well. 可能有更好的方法(我已经想到了),因此也可以建议它们。 anyways I got that part running easily, the problem is that whenever a user clicks again all the data is removed, so if they just want to adjust something they can't. 无论如何,我都能轻松地运行该部件,问题在于,每当用户再次单击时,所有数据都将被删除,因此,如果他们只是想调整某些内容,则无法调整。 to solve this (ill show code in sec) i put a check variable and an if. 为了解决这个问题(以秒为单位显示代码),我放入了一个检查变量和一个if。 this is what it looks like. 这就是它的样子。 (it doesn't work, btw) (不起作用,顺便说一句)

var unumber = 0;
var pnumber = 0; 

 if(unumber<1){
  $('#username').click(function(){
  unumber = 1;
  $('#username').val('');
 });
 };

 if(pnumber<1){
   $('#password').click(function(){
   $('#password').val('');
   pnumber = 1;
   });
 };

what I'm assuming happens is that every time some one clicks the variables are reset, and this leads to a more general question if this is this case, why would the whole script, not just the event handler, run? 我假设发生的事情是每次单击一次都会重置变量,这会导致一个更普遍的问题,如果是这种情况,为什么整个脚本而不是事件处理程序都将运行? Im new to javascript so forgive me if this is a stupid question. 我是Java语言的新手,如果这是一个愚蠢的问题,请原谅我。 Anyways, this is a really simple script and there are better and more efficient ways to do it, but how can it be done this way? 无论如何,这是一个非常简单的脚本,并且有更好,更有效的方法来执行此操作,但是如何以这种方式完成操作呢?

Your check for number less than 1, should be within the click handler 小于1的支票应在点击处理程序中

var unumber = 0;
var pnumber = 0; 

$('#username').click(function(){
  if(unumber<1){
    unumber = 1;
    $('#username').val('');
  }
});

$('#password').click(function(){
  if(pnumber<1){
     pnumber = 1;
     $('#password').val('');
  }
});

Note that this is not very robust, it doesn't handle tabbing into the fields. 请注意,这不是很可靠,它不能处理制表符。 To fix that, handle the focus event. 要解决此问题,请处理focus事件。

Another problem is that you don't get the message back if you don't type anything and leave the field. 另一个问题是,如果您未输入任何内容并离开该字段,则不会收到该消息。 A better approach is to compare against the initial value when the field receives focus. 更好的方法是在字段获得焦点时与初始值进行比较。 If there's nothing in there when you leave the field, restore to the original value. 如果离开该字段时那里什么也没有,请恢复为原始值。

Here's a jQuery plugin for creating these placeholders : https://github.com/mathiasbynens/Placeholder-jQuery-Plugin 这是一个用于创建这些占位符的jQuery插件: https : //github.com/mathiasbynens/Placeholder-jQuery-Plugin

Also, newer browsers support a placeholder attribute that does exactly that http://www.paciellogroup.com/blog/2011/02/html5-accessibility-chops-the-placeholder-attribute/ 此外,较新的浏览器也支持placeholder属性,该placeholder属性的作用完全符合http://www.paciellogroup.com/blog/2011/02/html5-accessibility-chops-the-placeholder-attribute/

I'd say the best way of doing this is to have a clear button inside the text input. 我想说的最好的方法是在文本输入中添加一个清晰的按钮。 See here for an example : How do I put a clear button inside my HTML text input box like the iPhone does? 参见示例: 如何像iPhone一样在HTML文本输入框中放置一个清除按钮?

No need for the messy number checking, use data on the element in question 无需进行混乱的数字检查,使用有关元素上的数据

This should work for you. 这应该为您工作。

$('#username,#password').focus(function(){
  if(!$(this).data('seen')) {
    $(this).data('seen',true);
    $(this).val('');
  }
});

http://jsfiddle.net/DeZ7D/ http://jsfiddle.net/DeZ7D/

You could store the placeholder and replace back on blur if the user hasn't entered anything. 您可以存储占位符,如果用户未输入任何内容,则可以重新替换为模糊。 more detailed implementation here: 更详细的实现在这里:

http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html

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

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