English 中文(简体)
恢复初步案文的数值,如果价值长度为<0>
原标题:Restore initial textarea value with jquery if value length < n

I m new in jquery and javascript coding and Itries to written a function that.1/63/s an original Value on a textarea if the length of its Value is less than "n" nature. 它没有工作。 你们能够解释我如何这样做?

该法典:

$("textarea").focus(function() {
    var text = $(this).val();
    $(this).val(  );
    $(this).blur(function() {
        if($(this).val().lenght<20)
        {
            $(this).val(text);
        }
    });
});
最佳回答

尽管效率不高,但这项工作将:

var text=  ;

$("textarea").focus(function() {
    text = $(this).val();
});

$("textarea").blur(function() {
    if($(this).val().length<20)
    {
        $(this).val(text);
    }
});
问题回答

页: 1

Once you fix that, you ll find that you re re-attaching the blur event each time but never removing it, meaning you will have tons of listeners attached. Try one or a more generic blur attached only once.

You need to create a closure with a local scope and pass in your variable so that it s local to that scope:

$("textarea").focus(function() {
    var text = $(this).val();
    $(this).val(  );
    (function(initialText) {
        $(this).blur(function() {
            if($(this).val().length < 20)
            {
                $(this).val(initialText);
            }
        });
    })(text);
});

If you don t create such a local scope with a closure, then event handler which you pass to .blur() won t know what text is, since it s not in the scope of this function.

http://jibbering.com/faq/notes/closures/“rel=“nofollow”http://jibbering.com/faq/notes/closures/

您可使用<代码>。 见下文。

$("textarea").one( focus , function() {      //will be executed once
    $(this).data( init_val , $(this).val()); //set data with initial value
    $(this).val(  );                         //clear the textarea
}).blur(function() {
    if($(this).val().length<20) {            //Check if entered text val is < 20
       $(this).val($(this).data( init_val ));//If yes, update with initial val
    }
});




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签