简体   繁体   English

使用JQuery获取textarea的默认值,清除onfocus并在empty上恢复值

[英]Using JQuery to get the default value of a textarea, clearing onfocus and reinstating value on empty

I have some JQuery that isn't working and I need a little help. 我有一些不起作用的JQuery,我需要一些帮助。 I a few forms on my website, and they all have a textarea with the class ".form-textarea". 我在我的网站上有一些表格,他们都有一个带有“.form-textarea”类的文本区域。 What I'm trying to do is use JQuery to get the default value of the textarea, clear the value on focus and reinstate the original value if the the textarea is empty. 我想要做的是使用JQuery获取textarea的默认值,清除焦点上的值并在textarea为空时恢复原始值。 I realise that an ID would probably be better but I need a generic function to affect all of the textareas with this particular class. 我意识到ID可能会更好但我需要一个泛型函数来影响这个特定类的所有textareas。

$(document).ready(function()
{
var def = $(".form-textarea")
$(".form-textarea").focus(function(srcc)
{
  if ($(this).val() == def)
    {
      $(this).removeClass("defaultTextActive");
      $(this).val("");
    }
});

$(".form-textarea").blur(function()
{
  if ($(this).val() == "")
    {
      $(this).addClass("defaultTextActive");
      $(this).val(def);
    }
});

$(".defaultText").blur();        
});

Here, give this a whirl and see if it does the trick. 在这里,给它一个旋转,看看它是否成功。

<script>
$(document).ready(function(){

    $default = "defaultText";

    $(".form-textarea").focus(function(){
        if( $(this).val() == $default ){
            $(this).removeClass("defaultTextActive");
            $(this).val("");
        }
    });

    $(".form-textarea").blur(function(){
        if( $(this).val() == "" ){
            $(this).addClass("defaultTextActive");
            $(this).val($default);
        }
    });
});
</script>

<input class="form-textarea" type="text" value="defaultText" />
<input class="form-textarea" type="text" value="defaultText" />

This is an old method I used for the exact same purpose. 这是我用于完全相同目的的旧方法。 I believe this is what you're looking for (uses Textareas) : Live demo 我相信这就是你要找的东西(使用Textareas): 现场演示

This uses the jQuery data API. 这使用jQuery数据API。 I've also added an extra class so you can markup your text nicely (disabled_text). 我还添加了一个额外的类,以便您可以很好地标记文本(disabled_text)。 This is a general purpose method so all you need to do is add the suggest class to your textarea/input and the script will do the rest 这是一个通用的方法,所以你需要做的就是将你的suggest类添加到textarea / input中,脚本将完成其余的工作。

<textarea class='suggest'>Some default value</textarea>
<textarea class='suggest'>Some default value2</textarea>
<textarea class='suggest'>Some default value3</textarea>
<input type='text' value ='me too' class='suggest'>

$('.suggest').each(function() {
        $this = $(this);
    if ($this.val() != '') {
        return;
    }
    $this.data('defaultval', $this.val());

    $this.addClass('disabled_text').focus(function() {

        if ($this.val() == $this.data('defaultval')) {
            $this.val('');

        }
        $this.removeClass('disabled_text');
    }).blur(function() {
        var oldVal = ($this.data('defaultval')) ? $this.data('defaultval') : '';
        if ($this.val() == '' && oldVal != '') {
            $this.addClass('disabled_text').val(oldVal);
        }
    })
});

我刚刚尝试从焦点功能中删除值“srcc”,它工作正常

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

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