检测jQuery函数所做的输入值更改 [英] Detect input value change made by jQuery function

查看:49
本文介绍了检测jQuery函数所做的输入值更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个函数可以更改包含在.jsp中的隐藏输入字段的值.

So I have a function that changes the value of a hidden input field which happens in an included .jsp.

在父.jsp中,我想检测到对此值的更改,但是它不起作用,但是我尝试了一下.

In the parent .jsp I want to detect a change to this value but it's not working however I try it.

请参阅jsfiddle中的示例.

See example in jsfiddle.

http://jsfiddle.net/3kxzvw5q/6/

$("input").change(function () {
    alert("input changed, calling registerChange");
    registerChange();
}); 

function registerChange(){
    $("#fieldsChangedIndicator").val('true');
}

$("#fieldsChangedIndicator").change(function() {
    alert("called");
});

注册更改后,我不想立即执行操作的原因是因为我希望在父jsp中执行操作.

The reason I don't want to perform the action immediately when the change is registered is because I want the action to be performed within the parent jsp.

将jsFiddle更新为更具描述性的示例

Updated the jsFiddle to a more descriptive example

推荐答案

概述

在回答我对您的问题的提问时,您说:

Overview

In reply to my question on your question, you said:

我正在尝试检测通过代码对该值所做的更改.

I'm trying to detect changes made to the value via code.

发生这种情况时不会触发任何事件.您要么必须触发一个或轮询,要么不必担心更改,直到/除非您必须这样做(您的用例似乎不允许这第三个选项).

There is no event triggered when that happens. You either have to trigger one, or poll, or don't worry about the change until/unless you have to (your use case doesn't seem to allow this third option).

如果您控制设置值的代码,则jQuery可以轻松触发和处理您自己的事件.设置值时:

If you control the code setting the value, jQuery makes it easy to trigger and handle your own events. When setting the value:

$("#someField").val("newValue").trigger("code-change");

要处理更改:

$("some relevant selector").on("code-change", function() {
    // Handle it here
});

尽管您可以触发change事件(只需更改上面的名称),但是请注意,如果处理该事件的代码可能期待真正的最终用户事件,则会产生意想不到的后果.

Although you can trigger the change event (just change the name above), beware unintended consequences if code handling the event may be expecting a real end-user event.

(function() {
  "use strict";
  
  // Watch for 'change'
  $("input").on("change", function() {
    display("Saw 'change'");
  });
  
  // Watch for 'code-change'
  $("input").on("code-change", function() {
    display("Saw 'code-change'");
  });
  
  // Watch for either
  $("input").on("change code-change", function() {
    display("Saw either 'change' or 'code-change'");
  });
  
  // Change it on button press
  $("#btn-change").on("click", function() {
    var el = $("#theElement");
    var val = el.val();
    switch (val.length) {
        case 0:
          val = "some value";
          break;
        case 1:
          val = val + val;
          break;
        default:
          val = val.substring(1) + val.substring(0, 1);
          break;
    }
    el.val(val).trigger("code-change");
  });
  
  function display(msg) {
    $("<p>").html(String(msg)).appendTo(document.body);
  }
})();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>Experiment with changing the value as a user (remember to leave the field), and with changing it using the button.</p>
<div><input id="theElement" value="Initial value" type="text"></div>
<div><input id="btn-change" type="button" value="Change It"></div>

如果您在处理代码时无法控制设置值,则主动"通知的唯一真正替代方法是轮询(漂白).请注意,每隔(例如)100毫秒进行一次轮询即可提供合理的响应,而不会产生大量开销,除非您检查的是真正的巨大字段数(这将引发用户应如何处理的问题成千上万的表单字段).

If you're dealing with code you don't control that's setting the value, your only real alternative for "proactive" notification is to poll (blech). Mind you, polling every (say) 100ms offers reasonable responsiveness without a lot of overhead unless you're checking a truly huge number of fields (which would raise the question of how the user was meant to deal with thousands and thousands of form fields).

这篇关于检测jQuery函数所做的输入值更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆