2017-07-07 64 views
0

我试图改变任何现有的文本与任何输入中键入。我尝试从id =“text”中取出它,但它不起作用,只显示文本。从输入ID更改文本

<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> 
+0

'$( “#响应”)。文字($(“#获得价值文本“)VAL());' – Satpal

回答

0

截至目前,你只是通过字符串#text因此它显示。使用.val()input获取值,然后进行设置。

$("#response").text($("#text").val()); 
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

<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>

使用$("#text").val()从输入