2012-08-17 65 views
2

我想创建两个文本框,如果有人写了一些文本 前50个字符应该在第一个文本框中 接下来输入的内容应该去 下一个文本框两个文本框一个字符数有限其他文本框剩余的

jQuery中或Javascript

enter image description here

的代码看起来是这样的

VAR的content_id = 'editable_div';
max = 50;

//binding keyup/down events on the contenteditable div 
$('#'+content_id).keyup(function(e){ check_charcount(content_id, max, e); }); 
$('#'+content_id).keydown(function(e){ check_charcount(content_id, max, e); }); 

function check_charcount(content_id, max, e) 
{ 
    if(e.which != 50 && $('#'+content_id).text().length > max) 
    { 
     //the remaining part focusing the mouse on to the next div let the id of the next editable div be next 

     e.preventDefault(); 
    } 
} 
+5

SO是问问题,而不是某个地方来记录您的愿望清单的地方。证明你至少试图建立这个系统,我们将尽力帮助解决它。但是现在你只是另一个“给编码”的freeloader。 – 2012-08-17 17:05:44

回答

2

我认为用户打字。 http://jsfiddle.net/sechou/fwU8D/

$("textarea:eq(0)").keyup(function(){ 
    if($(this).val().length>=10){ //for test: 10 
      var str =$(this).val(); 
      var length = str.length; 
      $("textarea:eq(0)").val(str.slice(0,10)); 
      $("textarea:eq(1)").val($("textarea:eq(1)").val()  
            +str.slice(10,length)); 

    } 
}); 
$("textarea:eq(0)").keydown(function(){ 
    if($(this).val().length>=10){//for test: 10    
      var str =$(this).val(); 
      var length = str.length; 
      $("textarea:eq(0)").val(str.slice(0,10)); 
      $("textarea:eq(1)").val($("textarea:eq(1)").val()  
            +str.slice(10,length)); 

    } 
}); 
$("textarea:eq(0)").change(function(){ 
    if($(this).val().length>=10){//for test: 10 
      var str =$(this).val(); 
      var length = str.length; 
      $("textarea:eq(0)").val(str.slice(0,10)); 
      $("textarea:eq(1)").val(str.slice(10,length)); 
      $(this).attr("readonly","readonly"); 
    } 
}); 

HTML

<textarea id="first"></textarea> 
<textarea id="second"></textarea> 
+0

是的,你是对的。没有注意到。 – insertusernamehere 2012-08-17 17:15:37

+1

Thanx为您的通知 – 2012-08-17 17:20:54

+0

真棒@insertusernameherehere 1.如果用户粘贴文本2.他不能编辑文本,如果文本大于50 – ZenOut 2012-08-17 17:27:31

相关问题