admin管理员组文章数量:1420220
I've got a bit of Javascript which the end goal is for whenever the "enter" key is pressed, a message is appended to my unordered list. Right now, any key that is pressed is appended rather than it just being the "enter" key. For example if I typed "Y", that would be submitted on the press of any other button. Obviously, all I want is data thats been typed into the text-box, submitted through "enter".
My HTML:
<ul id="messagebox" >
</ul>
<div>
<input type="text" id="typetextbox" maxlength="120" autoplete="off" />
<button type="submit" id="submit" onblur="submit"> </button>
</div>
Here is the button trigger piece of Javascript:
$(document).ready(function(){
$('#typetextbox').keypress(function (e){
if(e.keyCode == 13 );
$('#submit').click();
});
});
And here is the Javascript that works for appending the messages:
$(document).ready(function(){
$('#submit').click(function(){
var message = $('#typetextbox').val();
$('#messagebox').append(message + "<br/>");
$("#typetextbox").val("");
});
});
Why isn't it singling out just the Enter button to submit?
I've got a bit of Javascript which the end goal is for whenever the "enter" key is pressed, a message is appended to my unordered list. Right now, any key that is pressed is appended rather than it just being the "enter" key. For example if I typed "Y", that would be submitted on the press of any other button. Obviously, all I want is data thats been typed into the text-box, submitted through "enter".
My HTML:
<ul id="messagebox" >
</ul>
<div>
<input type="text" id="typetextbox" maxlength="120" autoplete="off" />
<button type="submit" id="submit" onblur="submit"> </button>
</div>
Here is the button trigger piece of Javascript:
$(document).ready(function(){
$('#typetextbox').keypress(function (e){
if(e.keyCode == 13 );
$('#submit').click();
});
});
And here is the Javascript that works for appending the messages:
$(document).ready(function(){
$('#submit').click(function(){
var message = $('#typetextbox').val();
$('#messagebox').append(message + "<br/>");
$("#typetextbox").val("");
});
});
Why isn't it singling out just the Enter button to submit?
Share Improve this question edited Aug 4, 2014 at 17:01 cja asked Aug 4, 2014 at 16:53 cjacja 471 silver badge11 bronze badges3 Answers
Reset to default 2keyCode
is actually a property of the event, so you have to use e.keyCode
.
Also you were calling if(keyCode == 13 );
: that semicolon (;
) right after the closed parenthesis, ends the if-statement immediately, so the next line of code is always executed.
Here is the correct code:
$('#typetextbox').keypress(function (e){
if(e.keyCode == 13 ) $('#submit').click();
});
To append the message to the ul
element you should create a li
element first, then append it to the ul
element.
Here is the correct code:
$('#submit').click(function() {
var message = $('#typetextbox').val();
if (message.replace(/ /g, '')) // If the text is not empty (e.g. nothing or only spaces)
$('#messagebox').append( $('<li>', {text: message}) );
$("#typetextbox").val("");
});
By the way, you don't need the $(document).ready
function to do this. Just put your script after the textarea in the dom and it will work fine.
HERE'S THE FIDDLE: http://jsfiddle/t7q4j/1/
update html code as
<div>
<input type="text" id="typetextbox" onkeypress="return keyPress(event)" maxlength="120" autoplete="off" />
<button type="submit" id="submit" onblur="submit"> </button>
</div>
java script implementation:
for every keypress the function keyPress(event)
will be called.
the function definition can be
var keyPress = function(e){
if(e.keyCode == 13){
send();
}
return true;
}
the function send consists of code for updating the message box
Try below code, it's working properly.
$(document).on("keypress", function (event) {
if (event.keyCode === 13) {
alert('You hit me')
}
})
本文标签: submitEnter key javascript keypress not workingStack Overflow
版权声明:本文标题:submit - Enter key javascript keypress not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745324026a2653508.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论