记录日常点点滴滴,欢迎来到我的小站。

0%

jQuery textarea判断回车提交和shift+回车换行的功能

html

1
2
3
<textarea id="comment">

</textarea>

Jquery

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
$('#comment').keydown(function(e){
var curKey = e.which;
if(e.shiftKey && e.which == 13 ){
var curr = getCaret(this);
var val = jQuery(this).val();
var end = val.length;
//jQuery(this).val( val.substr(0, curr) + 'n' + val.substr(curr, end));
}else if(curKey == 13){
$('#commentform').submit();
return false;
}
});

var str;
str = (window.location.search).substring(1);
if(str){
$('.rank_up_test').removeClass('showme');
var classname = '#middle_rank_up_test_'+ str + ' .rank_up_test';
console.log(classname);
$('#middle_rank_up_test_' + str).show();
}

});

function getCaret(el) {
if (el.selectionStart) {
return el.selectionStart;
}else if (document.selection) {
el.focus();
var r = document.selection.createRange();
if (r == null) {
return 0;
}
var re = el.createTextRange(),
rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint('EndToStart', re);
return rc.text.length;
}
return 0;
}