开发者

Copying text from textarea to another textarea

I have two textareas (ta1 and ta2). What I want is ta2 to mimic the text in ta1. This is easy but I need some other functionality as well.

ta1 has a keypress (jQuery) event which checks for certain words and replaces them. For instance if I type something in ta1 it will be replaced with something else. What I want is to store the original text typed in 开发者_运维百科ta2, without the replacement of text.

This is easy if all the user is doing is typing characters or pressing backspace. The problem comes when a user moves the cursor to somewhere in the middle of the text and starts typing. Or when the user selects a bunch of text and deletes it and starts typing. Does anyone know of a solution to this?


EDIT: To make this clearer this is what I am using this for. I am wanting something similar to Facebooks "Whats on your mind?" feature.

So a user can type @something and when they select from dynamically generated dropdown list it will replace @something with something. So say the user types "Hello @something how are you?" then the text displayed will become "Hello something how are you?"

I still however need to keep the "Hello @something how are you?" somewhere as I have an underlying div which styles "something" to have a background color. It would obviously also need to work for things like "@firstname secondname". Hope that clears things up a little.


UPD: This code dynamically updates text in t1, replacing tags like this: "[b]sometext[/b]" => "<b>sometext</b>" and copies "[b]sometext[/b]" to t2.

<?php
    if (array_key_exists("encode", $_GET) && strlen($_GET["encode"]) > 0)
    {
        $text = $_GET["encode"];
        $s = preg_replace("/\[b\](.*)\[\/b\]/i", "<b>\\1</b>", $text);

        echo $s;

        die;
    } else
    if (array_key_exists("decode", $_GET) && strlen($_GET["decode"]) > 0)
    {
        $text = $_GET["decode"];
        $s = preg_replace("/<b>([^<]{1,})<\/b>/i", "[b]\\1[/b]", $text);

        echo $s;

        die;
    } else
    if ((array_key_exists("decode", $_GET) && strlen($_GET["decode"]) <= 0) || (array_key_exists("encode", $_GET) && strlen($_GET["encode"]) <= 0))
    {
        echo "";

        die;
    }
?>

<html>
 <head>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">
   $(document).ready(function() {
    $("[name=t1]").keyup(function() {
     $.get("index.php?", {"encode": $("[name=t1]").val()}, function(data) {
      $("[name=t1]").val(data);
     });

     $.get("index.php?", {"decode": $("[name=t1]").val()}, function(data) {
      $("[name=t2]").val(data);
     });
    });
   });
  </script>
 </head>

 <body>
  <textarea name="t1"></textarea><br /><hr /><br />
  <textarea name="t2"></textarea><br /><br />
 </body>
</html>

Note: used JQ 1.4.2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜