检测字段是否使用JavaScript或jQuery更新

时间:2018-08-31 17:44:09

标签: javascript jquery

我有一个非常广泛的模板,并且有一种形式可以使用JavaScript或jQuery更新文本字段的值,该函数无法找到它,我需要检测何时更新该字段,我已经尝试了所有这些功能,但无法检测到何时更新。

为什么从JavaScript更新该字段时未检测到该字段,而在我在字段外编写并单击时则检测到该更新的原因是什么?

重要:动态添加的值90,000“使它成为特定功能,我一直无法找到它,并试图检测该值是否随JavaScript更改。 / p>

$(function(){

// Automatic update, strange function
setTimeout(function(){
  // Value updated automatically
  $('#long').val("90.000");
}, 2000);

/**
 * Detect if that field is updated
 */
$('input#long').on('change', function(){
  alert("Updated");
});

$(':input').on('change', function(){
  alert("Updated");
});

$('input#long').change(function(){
  alert("Updated");
});

$(document).on('change', 'input#long', function(){
  alert("Updated");
});  

$(document).on('change', 'input', function(){
  alert("Updated");
}); 

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="long">

5 个答案:

答案 0 :(得分:1)

使用var changeimage = function() { document.getElementById('image').src = this.options[this.selectedIndex].value + "_Image.png" } fencelist = document.getElementsByClassName('styleclass'); // all selects contains this class fencelist.addEventListener('change', changeimage, false); #include <stdio.h> #define MAXC 100 int main(void) { int c = 0; size_t n = 0; printf ("\n Enter a sentence.\n\n input: "); /* read up to 100 characters from stdin, print each word on a line */ while (n < MAXC && (c = getchar ()) != EOF && c != '\n') { if (c == ' ') printf ("\n"); else printf ("%c", c); n++; } printf ("\n"); if (n == MAXC) /* read and discard remaining chars in stdin */ while ((c = getchar ()) != '\n' && c != EOF); return 0; } 代替keyup。参见keyup()

paste
change

change()

  

change 事件在其值更改时发送到元素。这个   事件仅限于 input 元素, textarea 框和 select   元素。对于选择框,复选框单选按钮,当用户使用鼠标进行选择时,事件会立即触发,   ,但对于其他元素类型,事件将推迟到   元素失去焦点。

答案 1 :(得分:1)

您可以使用jquery中的change方法订阅事件。

html

<input class="target" type="text" value="Field 1">

和脚本

$(".target").change(function() {
  alert( "Handler for .change() called." );
});

See this codepen
See the jquery docs

答案 2 :(得分:1)

DOM事件不会由JS / jQuery调用触发。您可以轻松地手动触发change事件,如下所示:

$('#long').val('90.000').trigger('change')

示例

$(function(){

// Automatic update, strange function
setTimeout(function(){
  // Value updated automatically
  $('#long').val("90.000").trigger('change');
}, 2000);

/**
 * Detect if that field is updated
 */
$('input#long').on('change', function(){
  alert("Updated");
});

$(':input').on('change', function(){
  alert("Updated");
});

$('input#long').change(function(){
  alert("Updated");
});

$(document).on('change', 'input#long', function(){
  alert("Updated");
});  

$(document).on('change', 'input', function(){
  alert("Updated");
}); 

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="long">

答案 3 :(得分:1)

您是否尝试过在释放键时直接触发键触发触发器?

$(function(){

// Automatic update, strange function
setTimeout(function(){
  // Value updated automatically
  $('#long').val("90.000");
}, 2000);

/**
 * Detect if that field is updated
 */

$( "#long" ).keyup(function() {
  alert( "Handler for .keyup() called." );
});

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="long">

答案 4 :(得分:1)

因此,根据您对其他答案的编辑/评论,您无法手动触发Expand-Archive : The path 'C:\build\node-v-win-x64.zip' either does not exist or is not a valid file system path.

change是解决此问题的好方法,除非他们无法观察输入值的变化,as explained here

据我所知,您唯一的出路是使用MutationObserver每隔几毫秒将当前值与旧值进行比较。有点丑陋,根本不是最佳选择,但确实可以做到。这是一个示例:

setInterval
$(function() {
  setTimeout(function() {
    $('#long').val('90.000');
  }, 1000);
  
  $('#long').on('change', function() {
    alert('changed');
  });
  
  // Store the current value
  $('#long').data('oldVal', $('#long').val());
  
  // Every 100ms, trigger `change` event whenever new value != old
  setInterval(function() {
    if ($('#long').val() !== $('#long').data('oldVal')) {
      $('#long').trigger('change');
      $('#long').data('oldVal', $('#long').val());
    }
  }, 100);    
});