简体   繁体   中英

i don't understand why keycode ==13 is not working in chrome

For some reason my code below is not working even it is identical with someone's code that is actually running ok in chrome. I am using chrom and jquery 1.10.1 the keycode just doesn't fire.

http://jsfiddle.net/2m36v/

<ul id="tasks"></ul>
<input type="text" id="taskText" >

$(function() {
    $('#taskText').keydown(function(evt) {
        if (evt.keyCode == 13) {
            var taskText = this.value;
            $('<li>').text(taskText).appendTo('#tasks');
            $(this).val() = "";
        }
    });
}); 

You have a typo: $(this).val() = ""; Firebug shows that Chrome is throwing a parse error, so the script will not run at all. You need of course:

$(this).val("");

(Fiddle)

Change it

$(this).val() = "";

to

this.value="";

Fiddle DEMO

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