我们在问题中得到了第一部分但是,如果我不输入足够长的消息,我希望我的代码与以前保持同一条线。
以下是我所说的:
$(document).ready(function() {
var para = $('#UserInput');
$('textarea').keyup(function(e){
var length = this.value.length;
var newval = this.value.replace(/(?:\r\n|\r|\n)/g, '<br />');
if (e.keyCode == 13 && length > 6 ){
para.append(newval);
this.value = "";
}
if (e.keyCode == 13 && length < 6){
alert("Not long enough!");
}
});
});
例如,如果我输入He
,但是,我打算输入Hello
这是结果:
He \\"Alert Message"
llo \\"Prints 'He' on the first line and 'llo' on the second line, if I don't backspace."
预期结果:
He \\"Alert Message"
Hello \\(ON THE SAME LINE) "Prints 'Hello' on the first line"
答案 0 :(得分:1)
尝试用var length = this.value
替换$("textarea").length
来存储.length
textarea
的{{1}};在value
条件下纠正语法错误;首先if
>
<
,if
<
>
{/ 1}}
if
$(document).ready(function() {
var para = $('#UserInput');
$('textarea').keyup(function(e) {
var val = this.value;
var length = val.length;
var newval = val.replace(/(?:\r\n|\r|\n)/g, "");
if (e.keyCode == 13 && length < 5) {
para.append(newval);
this.value = "";
}
if (length < 5) {
alert("Not long enough!");
}
});
});