开发者

jQuery fadeout woes

开发者 https://www.devze.com 2023-01-27 07:59 出处:网络
Using the keyup on jQuery, is there a way to check a what textfield currently has in it\'s box? I mean if the textbox currently does not have the word \'apples\' then do a jQuery function, then if it

Using the keyup on jQuery, is there a way to check a what textfield currently has in it's box?

I mean if the textbox currently does not have the word 'apples' then do a jQuery function, then if it does have the word apples... then do not do the jQuery function that happens when 'apples' is not typed.

I hope that made sense. I tried using CASE开发者_运维百科S and keyup but I couldn't get it to work.

Is this possible?


You can use jQuery's keyup and val() function after selecting the input:

HTML:

<input id="granny_smith"/>

JavaScript:

$("#granny_smith").keyup(function() {
    if ($(this).val() === "apples") {
        alert("apples");
        // Do apples function
    }
    else {
        // Do !apples function
    }
});

Working example: http://jsfiddle.net/uKnJB/

Hope that helps!


You should be looking at input if you're looking to capture user inputs into a textbox. Although the input event has cross-browser compatibility issues, it is better than using the key* set of events for this purpose. See AndyE's article on this for more information: http://whattheheadsaid.com/2010/09/effectively-detecting-user-input-in-javascript

A simple example of how you would use it is this:

$('#text').bind('input', function(){
    if(this.value.indexOf('apples') === -1){
        // Textbox doesn't contain 'apples'
        $('#apples').hide();
    } else {
        $('#apples').show();
    }
});

This would check the value (the text in the textbox) for a certain string and execute a function based on that. A simple demo of this can be found here: http://www.jsfiddle.net/yijiang/Xy6Cc/

0

精彩评论

暂无评论...
验证码 换一张
取 消