更改后如何获取textarea的textContent?

时间:2016-06-09 04:15:07

标签: javascript jquery

我有一个textarea标签。我希望能够在textContent更改后获得textContent吗?

<textarea id="xxx"> sameple text </textarea> // before
<textarea id="xxx"> sameple text and new text </textarea> // after

即使textarea的内容发生了变化,我也想听。

3 个答案:

答案 0 :(得分:1)

您可以将脚本设为

$(document).ready(function(){
  $("#xxx").change(function(){
     alert($("#xxx").val());
  });
});

如果您放置$("#xxx").html()它会在文本之前提供给您,但如果您保留$("#xxx").val()则会给您更改值。

答案 1 :(得分:0)

您需要使用onkeyup和onchange。 onchange将阻止上下文菜单粘贴,并且每次按键都会触发onkeyup。

$(document).ready(function(){
       $(document).on('change','#xxx',function(){
             var newtext = $(this).text();
       });
});

答案 2 :(得分:0)

  

使用input事件。

<?php session_start(); define('DB_HOST', '<im putting my host here>'); define('DB_NAME', '<im putting my db name here>'); define('DB_USER','<im putting my username here>'); define('DB_PASSWORD','<im putting my password here>'); $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error()); $db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error()); $text= ''; function NewUser() { $username= $_POST['username']; $email = $_POST['email']; $password = $_POST['password']; if($data) { $text= "Account created."; header('Location: login.php'); $text= "Account created."; } } function SignUp() { if(!empty($_POST['username'])) //checking the 'user' name which is from Sign-Up.html, is it empty or have some text { $query = mysql_query("SELECT * FROM members WHERE username = '$_POST[username]' AND password = '$_POST[password]'") or die(mysql_error()); $q = mysql_query("SELECT * FROM members WHERE username = '$_POST[username]'"); if(!$row = mysql_fetch_array($q)) { newuser(); } else { $text= "Account already exists under that username"; header('Location: createpage.php'); $text= "Account already exists under that username"; } } else { $text= "Account already exists under that username"; header('Location: createpage.php'); $text= "Account already exists under that username"; } } if(isset($_POST['submit'])) { SignUp(); } ?> DOM元素的值发生变化时,input event <input>会同步触发。

<textarea>
$('#xxx').on('input', function() {
  console.log(this.value);
});