2012-09-17 38 views
0

是否有可能在jquery mobile textbox中只有一部分文本是只读的?使textfield只读部分只读jquery mobile

如果下面的括号内是我的文本框,我想这个词“快乐”是碰不得

[happy...................] 

换句话说,人们应该能够删除所有这些时期,直到他们达到年。可能,还是不?

谢谢!

回答

0

我想你可能想尝试这样的事:

JS/jQuery代码:

// Your untouchable string 
var my_string = "happy"; 

$(function() { 

    // The following function makes sure that when the user is typing, // 
    // the user won't touch the word "happy" 
    $("#my_input").keyup(function() { 
     var string = $(this).val(); 

     if(string.length >= my_string.length) { 
      if(string.substring(0, my_string.length) != my_string) { 
       // This part makes sure that the user won't modify the word "happy" 
       $(this).val(my_string+string.substring(my_string.length+1, string.length)); 
      } 
     } else { 
      // This part makes sure that the user won't typed: 
      // - before the word "happy" (eg: "Zhappy") 
      // - inside the word "happy" (eg: "hapZpy") 
      $(this).val(my_string); 
     } 
    }); 
}); 

的HTML(例如):

<body> 
    <div data-role="page"> 
     <div data-role="content"> 

      <!-- The value of your input is initially set to your untouchable string --> 
      <input id="my_input" type="text" value="happy"/> 
     </div> 
    </div> 
</body> 

希望这帮助。