当前位置: 动力学知识库 > 问答 > 编程问答 >

jQuery button doesn't act on the live data

问题描述:

I want a very simple thing, to have an input and output text area, and then a button that makes javascript read the input area, then set the text of the output area in an appropriate way. I was told to use jQuery to do this, but it seems that the selection doesn't actually run each time the button is pressed.

My setup is like this:

<html>

<head><title>Example Input-Output</title></head>

<body>

<p><textarea id="inputArea" rows="7" cols="25">Input Text Here</textarea></p>

<p><button id="theButton" type="button">Activate</button></p>

<p><textarea id="outputArea" rows="7" cols="25">Output Text Here</textarea></p>

<script src="jquery.js"></script>

<script>

$(document).ready(function(){

$("#theButton").click(function(event){

var inputValue = $("#inputArea").text();

$("#outputArea").text("input was:\n"+ inputValue);

});

});

</script>

</body>

</html>

but when I click the button after having changed the text to anything other than the text it shows at page load, it still doesn't base the output on the current input text. I assume I'm making some dumb beginner's mistake?

网友答案:

You need to use .val() to get the value of textarea not .text()

$(document).ready(function() {
  $("#theButton").click(function(event) {
    var inputValue = $("#inputArea").val();
    $("#outputArea").text("input was:\n" + inputValue);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>
  <textarea id="inputArea" rows="7" cols="25">Input Text Here</textarea>
</p>
<p>
  <button id="theButton" type="button">Activate</button>
</p>
<p>
  <textarea id="outputArea" rows="7" cols="25">Output Text Here</textarea>
</p>
网友答案:

Get using .val() instead of .text()

$("#inputArea").val();

DEMO

分享给朋友:
您可能感兴趣的文章:
随机阅读: