简体   繁体   English

jQuery keyup不能通过javascript检测到内容更改

[英]Jquery keyup doesn't detect content changes by javascript

I have this code which puts the content of my input fields into a preview div area: 我有这段代码将输入字段的内容放入预览div区域:

JS JS

function fieldPreview() {
$('.input').on('keyup', function() {
    $('#' + $(this).data('id')).html(this.value);
});
}

HTML HTML

<input class="input" type="text" name="title" placeholder="Job Title" data-id="title">
<div id="title"></div>

My issue comes from the fact that two of my input fields are a little more complicated. 我的问题来自以下事实:我的两个输入字段有些复杂。 One field uses google map autosuggest. 一个字段使用Google地图自动建议。 When I click on the autosuggestion link it updates the text field but doesn't update the preview in the div until I actually modify the input manually by adding or removing another letter. 当我单击自动提示链接时,它会更新文本字段,但不会更新div中的预览,直到我实际上通过添加或删除另一个字母手动修改输入。

Is there a better way in jquery to monitor changes in an input field? jQuery中是否有更好的方法来监视输入字段中的更改? This will allow me to put the contents of the field into the div even if it has been put there by javascript rather than a keyup. 这将允许我将字段的内容放入div中,即使该字段的内容是通过javascript而不是通过keyup放置的。

Here is an example of jquery clearing the contents of the input field but it not being reflected in the DIV: http://jsfiddle.net/xDA9p/5/ 这是一个jquery清除输入字段内容但未反映在DIV中的示例: http : //jsfiddle.net/xDA9p/5/

Use the change paste keyup input events. 使用change paste keyup input事件。

$('.input').on('change paste keyup input', function() {
  $('#' + $(this).data('id')).html(this.value);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM