[英]how to change the value of a Div according to whats typed into a form input
I know if I want to change the value of an input on a form according to what a user types into another input using: 我知道我是否要根据用户使用以下命令输入的内容来更改表单上输入的值:
$('#inputName').change(function() {
$('#inputURL').val($('#inputName').val());
});
But I'd like to have it so when someone types into the input it updates a Div with the text instead. 但是我想拥有它,所以当有人在输入中输入内容时,它将使用文本更新Div。 IE they type into #inputName and this happens:
在IE中,他们输入#inputName并发生这种情况:
<div id="personsName"> text appears here as they type</div>
How'd I go about doing that? 我该怎么做呢? I doesn't work when I change #inputURL into #personsName :(
将#inputURL更改为#personsName时,我不工作:(
The change
event is only triggered when leaving the textfield, so use keypress
instead if you really want to "update the text on typing into a text field". change
事件仅在离开文本字段时触发,因此,如果您确实要“在键入文本字段时更新文本”,请使用keypress
。 .val
only applies to form elements which actually have a value (eg <input>
and <textarea>
elements). .val
仅适用于实际上具有值的表单元素(例如<input>
和<textarea>
元素)。 In all other cases, you need the text
method for modifying the text . 在所有其他情况下,都需要使用
text
方法来修改text。
$("#inputName").keypress(function () {
$("#personName").text(this.value);
});
You can test this code here: http://jsfiddle.net/ntBnA/ 您可以在此处测试此代码: http : //jsfiddle.net/ntBnA/
You're almost there: 你快到了:
$('#inputName').change(function() {
$('#personsName').text($('#inputName').val());
});
//Cache this bad boy, its being repeatedly used!
var personsName = $('#personsName');
$('#inputName').bind({
keyup: function () {
//This is what you want.
personsName.text($(this).val());
},
change: function () {
//This is what you were doing. See the difference?
$('#inputURL').val($(this).val());
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.