JavaScript文本区字符计数器插入标签


JavaScript textarea character counter inserting tab

当用户在我的网站上为他们的个人资料设置他们的简历时,有一个Javascript字符计数器显示他们还剩下多少字符。

当他们去写一个新的个人信息或编辑他们当前的个人信息时,在标签中,他们当前的个人信息将在PHP中从数据库中返回。下面是代码

<script>
  $(document).ready(function() {
    var text_max = 300;
    $('#textarea_feedback').html(text_max + ' characters remaining');
    $('#textarea').keyup(function() {
      var text_length = $('#textarea').val().length;
      var text_remaining = text_max - text_length;
       $('#textarea_feedback').html(text_remaining + ' characters remaining');
    });
  });
</script>
<form action="profile_edit_bio.php" method="post">
  <textarea id="textarea" name="bio" class="input-xxlarge" maxlength="300" placeholder="Update your bio! This should be a short paragraph explaining why you're awesome. (Max characters 300)">
    <?php if(strlen($row[20])!=0) {echo $row[20];}?>
  </textarea>
  <div id="textarea_feedback">
    If you're seeing this message, please <a href="mailto:support@jaycraft.co">contact support</a>
  </div>
  <button type="submit" class="btn btn-info">Update bio</button>
</form>

当他们点击表单中的提交按钮时,它成功地保存了个人简介和所有内容,但现在它显示了他们的个人简介和<?php if(strlen($row[20])!=0) {echo $row[20];}?>,并在其周围显示了大量的标签。

例如

你好,我叫James

将成为

                          Hello, my name is James

这个脚本肯定有问题,因为它没有将这些空格/制表符保存到数据库中

您正在自己插入制表符。<textarea></textarea>之间的任何都成为显示的可编辑文本的一部分。你的代码应该是

<textarea>$text_to_insert</textarea>

注意在标签和插入的变量/text之间没有任何空白。你的版本是

<textarea>[linebreak]
[tab][tab][tab]$text_to_insert[linebreak]
</textarea>