2015-12-03 32 views
0

例如,我有一個帶文本的textarea。jQuery - 允許更改textarea中的文本的一部分

<textarea id="response" name="response" class="form-control" maxlength="160">Text1. Text2</textarea> 

我需要允許用戶只編輯Text1Text2不應該改變。

+0

您可以嘗試檢測用戶何時更改文本('input'事件),然後追加'Text2'。但是,您需要切分字符串並查看最後一部分是否爲「Text2」。如果是,請不要附加文字。 – Druzion

+0

什麼是用戶將Text2更改爲Textss22。所以我會追加Text2?所以我會得到Text1。 Textss22。 Text2 – user3351236

+0

嗯......我沒有想到這一點。爲什麼你最終需要'Text2'?根據您的需要,您可以創建另一個具有'display:none'的textarea,並向其中添加Text2。然後,您可以加入用戶使用Text2鍵入的文本,並使用該字符串。由於textarea被隱藏,他們無法編輯Text2。 – Druzion

回答

0

你凸輪使text1的固定和可編輯文本2,但你必須採取的text1爲跨度,你可以看到例如HERE

它可以幫助你。

0

在提交表單(假設它)時,使用CSS和contenteditable標籤創建假textarea並從中提取數據可能會更好。例如:

<html> 
<head> 
    <style> 
     #custom-textarea { 
      border: solid 1px #999999; 
      resize: both; 
      overflow: auto; 
     } 

     #custom-textarea *:read-write:focus { 
      outline: none; 
     } 

     #custom-textarea span { 
      pointer-events: none; 
     } 
    </style> 
    <script src="jquery-1.11.3.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function(){ 
      $("#custom-textarea span").on("focus", function(){ 
       $(this).parent().trigger("focus"); 
      }); 
     }); 
    </script> 
</head> 
<body> 
    <div style="width: 165px;"> 
     <div id="custom-textarea" tabindex="-1"> 
      <span contenteditable="true">This section is editable.</span> 
      This section is not editable. 
     </div> 
    </div> 
    <textarea>This section is editable. This section is still editable.</textarea> 
</body> 
</html>