带有描述性文本的文本输入

发布于 2024-07-14 09:35:12 字数 304 浏览 11 评论 0原文

我想知道是否有一种简单的方法可以使用 javascript(包括我们在网站上使用的 JQuery)将描述性文本放入文本输入中,直到用户单击它来输入自己的文本。

例如,我想将“搜索”一词放入文本输入中(最好使用比实际输入更浅的颜色),直到用户单击输入,当它消失并允许他们输入搜索词时。

不过,我实际上并不希望“搜索”一词成为文本输入的值,因为用户可以搜索单词“搜索”有点重要。

我正在考虑绝对定位

在输入上包含单词搜索的元素,并在单击它(或输入)时将其隐藏。

你怎么认为? 这是严重的误导吗?

I was wondering if there's an easy way with javascript (including JQuery, which we're using on the site) to put descriptive text in a text input until the user clicks it to type in their own text.

For instance I'd like to put the word 'Search' in a text input (preferrably in a lighter color than real input) until the user clicks the input, when it disappears and allows them to type in their search terms.

I don't want to actually have the word 'Search' be the value of the text input though, because it is somewhat important that the user can search for the word search.

I was thinking of absolute positioning a <p> element with the word search over the input and hiding it when it (or the input) is clicked.

What do you think? is this horribly misguided?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

爱殇璃 2024-07-21 09:35:12

我最近遇到了 jQuery 表单示例插件,它正是您想要的。 该项目的 github 页面位于此处。 使用该插件,您可以显示一个占位符值,该值将在单击时消失:

$('input#search').example('Search');

I recently bumped into the jQuery Form Example plugin that does just what you want. The github page for the project is here. With that plugin, you can display a placeholder value that will vanish on click with just this:

$('input#search').example('Search');

时光礼记 2024-07-21 09:35:12

您可以将搜索输入的初始值设置为“搜索”,然后添加 onClick 侦听器以将其删除。

$("input.search-term").one("click", function() { $(this).removeAttr("value"); });

不会影响用户的搜索“搜索”的能力。 与尝试在输入上欺骗

元素相比,这是一种更简洁的方法。

编辑:你们都绝对正确 - 删除每次点击的价值是糟糕的用户体验。 这就是我快速回答的结果。 :) 更新为仅删除初始值。

You can set the initial value of the search input to "Search" and then add an onClick listener to remove it.

$("input.search-term").one("click", function() { $(this).removeAttr("value"); });

It won't impact the user's ability to search for "search." This is a much cleaner approach than trying to finagle a <p> or <label> element over the input.

EDIT: You are all absolutely right - removing the value on each click is poor UX. That's what I get for answering quickly. :) Updated to only remove the initial value.

海夕 2024-07-21 09:35:12

您可能想检查一下:
http://www.newmediacampaigns.com/page/nmcformhelper

基本上,HTML 5 支持输入的 placeholder 属性elements:

<input type="text" placeholder="Your browser supports placeholder text" size=38>

WebKit(Chrome 和 Safari)现在支持此功能,但对于 Firefox 和 IE 等其他浏览器,当浏览器不支持 HTML 5 功能时,您可以使用此脚本使用 Javascript 来模拟效果。

You may want to check this out:
http://www.newmediacampaigns.com/page/nmcformhelper

Basically, HTML 5 supports the placeholder attribute for input elements:

<input type="text" placeholder="Your browser supports placeholder text" size=38>

WebKit (Chrome and Safari) supports this now, but for other browsers like Firefox and IE, you can use this script to simulate the effect using Javascript when the browser doesn't support the HTML 5 feature.

遗心遗梦遗幸福 2024-07-21 09:35:12

For years there has been a popular "project" known as labels.js, orignally written by Aaron Boodman of the old youngpup.net. The idea is still around and there exists a jQuery version, even (demo #4).

倾城花音 2024-07-21 09:35:12

尽管如此,我实际上并不希望“搜索”一词成为文本输入的值,因为用户可以搜索单词“搜索”有点重要。

在这种情况下,您可以在文本输入上使用背景图像。 您可以在 CSS 中定义:焦点不应该有背景,而未聚焦的应该有背景。 即使禁用 javascript,此解决方案也能工作。

更新:已测试并适用于 FF、Opera 和 Chrome,但不适用于 IE,因为 IE 不支持 :focus,但 IE 支持背景图像,所以如果文本是浅灰色的,应该不是问题。 您始终可以使用 jquery 来更改输入字段应具有的类别。

I don't want to actually have the word 'Search' be the value of the text input though, because it is somewhat important that the user can search for the word search.

In that case you could use a background-image on the text-input. You could define in CSS that :focus should not have the background and that the unfoucused should have it. This solution works even if javascript is disabled.

Update: Tested and work with FF, Opera and Chrome but not IE, since IE don't support :focus, but IE supports the background image, so if the text is light gray is should not be a problem. And you can always use jquery to change what classed the input field should have.

七秒鱼° 2024-07-21 09:35:12

我不久前一直在寻找这个解决方案,并遇到了这个

/**
* @author Remy Sharp
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/

(function ($) {

$.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }

  return this.each(function () {
    // get jQuery version of 'this'
    var $input = $(this),

    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = $(this.form),
      $win = $(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title

      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};

})(jQuery);


$(function(){ 
    // find all the input elements with title attributes
    $('input[title!=""]').hint();
});

I was looking for this solution not long ago and came cross this

/**
* @author Remy Sharp
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/

(function ($) {

$.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }

  return this.each(function () {
    // get jQuery version of 'this'
    var $input = $(this),

    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = $(this.form),
      $win = $(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title

      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};

})(jQuery);


$(function(){ 
    // find all the input elements with title attributes
    $('input[title!=""]').hint();
});
牛↙奶布丁 2024-07-21 09:35:12

我会在 jQuery 中做类似的事情:

$("input.searchterm").click(function() { 
  var $input = $(this);
  if ($input.val() == "enter search term") $input.val("");
};

这样用户再次单击时就不会丢失之前输入的内容。 仅当该值是您分配给输入字段的默认值时才会被清除。

I would do something like this in jQuery:

$("input.searchterm").click(function() { 
  var $input = $(this);
  if ($input.val() == "enter search term") $input.val("");
};

This way users won't lose what they have typed previously upon clicking again. The value would only be cleared if it is the default value you assign to the input field.

紫竹語嫣☆ 2024-07-21 09:35:12

作为参考,这种技术通常称为水印

For reference, this technique is often called watermarking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文