从输入ID更改文本

时间:2017-07-07 11:10:27

标签: jquery html

我尝试使用输入中键入的内容更改任何现有文本。我试着从id =" text"但它不起作用,只显示文字。

<input type="text" class="form-control" id="text" placeholder="Type your text here.....">

<div class="form-group">
    <button>submit</button>
</div>
<div class="form-group" id="response">
    <pre>Type some text in the above text field, then log the text out here on button click</pre>
</div>


<script>
    $('button').click(function() {
        $("#response").text("#text");
    });
</script>

3 个答案:

答案 0 :(得分:1)

 $('button').click(function() {
 
    $("#response").text($("#text").val());
    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<input type="text" class="form-control" id="text" placeholder="Type your text here.....">        

  <div class="form-group">
    <button>submit</button>
  </div>  
  <div class="form-group" id="response">
    <pre>Type some text in the above text field, then log the text out here on button click</pre>
  </div>  



<script>
 
</script>

答案 1 :(得分:1)

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="form-control" id="text" placeholder="Type your text here.....">        

  <div class="form-group">
    <button>submit</button>
  </div>  
  <div class="form-group" id="response">
    <pre>Type some text in the above text field, then log the text out here on button click</pre>
  </div>  



<script>
  $('button').click(function() {
    $("#response").text($("#text").val());
});
</script>
&#13;
&#13;
&#13;

使用$("#text").val()从输入中获取值

答案 2 :(得分:0)

截至目前,您只是传递字符串#text,因此会显示它。使用.val()input获取值,然后设置它。

 $("#response").text($("#text").val());