简体   繁体   中英

Executing JavaScript on Key Press

I'm doing some very rudimentary javascript work and seem to hit a bump. For the life of me, I can't seem to find what should be a very basic answer on Google.

<html>
    <head>
        <title>Test Page</title>
        <script type="text/javascript">
    function lowerCase(){
        var input = document.send.inputText;
        input.toLowerCase();
        document.write(input);
        alert(input);
    }
    </script>
    </head>
    <body>
        <h1>Test Page</h1>
        <form name="send">
            <input type='text' name="inputText" onkeypress="lowerCase()"/>
        </form>
    </body>
</html>

My intent is that the function lowerCase is executed on entering information into the textbox and pressing enter. However, I can never seem to get the function to execute.

How about...

HTML:

<input type='text' name="inputText" onkeypress="lowerCase(this)">

JavaScript:

function lowerCase ( input ) {
    setTimeout( function () {
        input.value = input.value.toLowerCase();    
    }, 0 );    
}

Live demo: http://jsfiddle.net/vXpj8/3/


function lowerCase ( e ) {
    if ( e.keyCode === 13 ) {
        this.value = this.value.toLowerCase();
        e.preventDefault();
    }        
}

document.send.inputText.onkeypress = lowerCase;

Live demo: http://jsfiddle.net/vXpj8/1/

Notice, how I bind the event handler with JavaScript. I do this because I want to have the event object available in the function.

There's a space between the onkeypress attribute and equals sign. Remove that; it should work.

Fiddle: http://jsfiddle.net/iambriansreed/jEnxH/

<form name="send">
    <input type='text' name="inputText">
</form>
<script>
    document.send.inputText.onkeypress = function(event){
        if(event.keyCode != 13) return;       
        this.value = this.value.toLowerCase();
        alert(this.value.toLowerCase());
        event.preventDefault();
    };
</script>

If you want it to work when the enter key is pressed, then you'll need to deal with the form being submitted, since pressing enter in a text input element that is a form control submits the form.

More likely you want to change the value to lower case on some other event, such as keup, eg

 <input onkeyup="this.value = this.value.toLowerCase();" ... >

Doing this sort of thing is a bit annoying for users though, since upper case letters are magically changed to lower case. If there is a back-end requirement for lower case letters, better to deal with it there than confuse users entering text.

a few issues with your code first var input is a input box not the string, toLowerCase() is a string method, you need input value

var input = document.send.inputText;
alert(input.value);

second, onkeypress is excuted before text is entered, maybe you should consider change onkeypress to onkeyup

try if this helps

<html>
<head>
    <title>Test Page</title>
    <script type="text/javascript">
        function lowerCase(){
            var input = document.send.inputText;
            input.value = input.value.toLowerCase();
        }
    </script>
</head>

<body>

    <h1>Test Page</h1>
    <form name="send">
        <input type='text' name="inputText" onkeyup="lowerCase()">
    </form>

</body>
</html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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