JavaScript ile textarea içine girdi alanı metin ekleme

2 Cevap php

Ben zaten içeride bazı metin var metin alanı içine girdi alanından bir metin / dize eklemek gerekir. Takılı dize imleç konumunda olmalıdır, ve eklemek için bir düğme gereklidir.

JavaScript ile bunu gerçekleştirmek için bir yolu var mı?

Önceden thx

P.S. girilmelidir metin / dizesi PHP ile veritabanından getirilen

2 Cevap

@ Kamia kod örneği doğru yolda olduğunu. İşte test için tam bir uygulama bulunuyor. Senin açıklamasına dayanarak, ben senin gerçek kodda muhtemelen textarea eklemek için metni almak php kullanarak arama bazı türüne sahip olacağını varsayarak yaşıyorum. Bu durumda ise, o zaman sunucu Ajax çağrının bir tür gerektirecektir. Bunun için jQuery's Ajax features kullanarak öneriyoruz.

<html>
 <head>
  <title>Test Page</title>
  <script type="text/javascript">
    window.onload = function(){
       btn = document.getElementById("btnInsertText");
       myText = document.getElementById("myTextArea");
       text = document.getElementById("textToInsert");
       btn.onclick = function(){
          insertAtCursor(myText, text.value);
       }
    }

    function insertAtCursor(myField, myValue) { 
    	//IE support 
    	if (document.selection) { 
    		myField.focus(); 
    		sel = document.selection.createRange(); 
    		sel.text = myValue; 
    	}
    		//Mozilla/Firefox/Netscape 7+ support 
    	else if (myField.selectionStart || myField.selectionStart == '0'){  
    		var startPos = myField.selectionStart; 
    		var endPos = myField.selectionEnd; 
    		myField.value = myField.value.substring(0, startPos)+ myValue 
                 + myField.value.substring(endPos, myField.value.length); 
    		} else { 
    			myField.value += myValue; 
    		} 
    	}       
    </script>
 </head>
 <body>
   Text To Insert:<input type="text" id="textToInsert" />
   <input type="button" id="btnInsertText" value="Insert Text" /><br/>
   <textarea id="myTextArea" rows="6" cols="50">
      Contrary to popular belief, Lorem Ipsum is not simply random text. 
      It has roots in a piece of classical Latin literature from 45 BC, 
      making it over 2000 years old.
   </textarea>
 </body>
 </html>

Dede

bazı temel googling: D ->

 <script type="text/javascript"> 
<!-- 

//myField accepts an object reference, myValue accepts the text strint to add 
function insertAtCursor(myField, myValue) { 
//IE support 
if (document.selection) { 
myField.focus(); 

//in effect we are creating a text range with zero 
//length at the cursor location and replacing it 
//with myValue 
sel = document.selection.createRange(); 
sel.text = myValue; 
} 

//Mozilla/Firefox/Netscape 7+ support 
else if (myField.selectionStart ¦¦ myField.selectionStart == '0') { 

//Here we get the start and end points of the 
//selection. Then we create substrings up to the 
//start of the selection and from the end point 
//of the selection to the end of the field value. 
//Then we concatenate the first substring, myValue, 
//and the second substring to get the new value. 
var startPos = myField.selectionStart; 
var endPos = myField.selectionEnd; 
myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length); 
} else { 
myField.value += myValue; 
} 
} 

//--> 
</script>

Metin eklemek için düğmesini kullanın:

<textarea id="mytext" rows="8" cols="70"></textarea> 
<button onclick="insertAtCursor(document.getElementById('mytext'),'Test text')" valueAdd Test text>

Bu seni umut olur

C ya