我需要清空並專注於按鍵上的textarea按鍵是enter.i我認爲我寫了正確的代碼,但有時候textarea變空了,但沒有發生焦點,有時會發生焦點,但沒有運氣排空這個object.i使用了很多不同的代碼,但沒有運氣。輸入空白並專注於textarea
這裏是我的JS:
<script>
$(document).ready(function() {
$("#data").focus(function() {
$(this).empty();
$(this).focus();
});
})
// when the client hits ENTER on their keyboard
$('#data').keypress(function(e) {
$this = $(this);
if($this.val().length == 1)
{
var x = new RegExp("[\x00-\x80]+"); // is ascii
var isAscii = x.test($this.val());
if(isAscii)
{
$("#data").css({"direction": "ltr", "text-align": "left"});
} else {
$("#data").css({"direction": "rtl", "text-align": "right"});
}
}
if (e.shiftKey && e.keyCode == 13) {
$(this).val($(this).val() + "\n");
return;
}
if(e.which == 13) {
if($this.val().length == 0){
alert('your value is empty')
}
var now = time();
if(now+0.5 > counter){
counter = now;
$(this).blur();
$('#datasend').focus().click();
}else{
}
}
});
});
,這裏是我的html部分:
<div class="wrap">
<div class="rooms">
<div class="clear"></div>
<div class="roomsicon">
chatrooms
</div>
<ul id="rooms">
</ul>
</div>
<div class="chat_box_container">
<div class="chat_box" id="mydiv">
<div id="conversation"></div>
</div>
<div class="input_field">
<textarea id="data" placeholder="type your text ... " ></textarea>
<input type="button" id="datasend" value="send" />
</div>
</div>
<div class="users">
<div class="usersicon">online users </div>
<ul id="users">
</ul>
</div>
</div>
<!-- clear float -->
<div class="clear"></div>
撥弄 – PraJen 2013-05-07 13:12:55
份額輸入的鍵碼是13. – tymeJV 2013-05-07 13:14:53
e.which == 13,我想我用'13太...沒辦法分享小提琴,因爲它是一個聊天系統當我sumbit聊天消息將apear在聊天室 – HiDd3N 2013-05-07 13:17:01