即使通过 jquery 更改值,jquery 输入字段也会更改

发布于 2025-01-04 07:52:35 字数 483 浏览 4 评论 0原文

我试图捕获文本输入字段的值何时更改。我正在使用:

$("input[name=color]").change(function(){

如果我直接输入新值,这会起作用。然而,输入字段的值正在被 jquery 更改。其中有几个字段,我需要知道任何字段何时发生更改。有没有办法检测到这种变化?

抱歉,我没说清楚。我不是改变价值的人。这是一个插件,我不想修改它,以防我需要将其移植到另一个项目。

解决方法

好的,所以你不能做我想做的事情,但这里有一个解决方法。我只是做了一个间隔并将我的 change 事件更改为 each 事件。

setInterval(function(){
    $("input[name=color]").each(function(){ my code })
},100);

I am attempting to capture when a text input field's value is changed. I am using:

$("input[name=color]").change(function(){

This works if I type the new value in directly. However, the value of the input field is being changed by jquery. There are several of these fields, and I need to know when any have changed. Is there a way to detect this change?

Sorry, I wasn't clear. I'm not the one changing the value. It's an addon that I would rather not modify in case I need to port it to another project.

WORK-AROUND

Ok, so you can't do what I wanted to do, but here is a work-around. I just made an interval and changed my change event to an each event.

setInterval(function(){
    $("input[name=color]").each(function(){ my code })
},100);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦醒时光 2025-01-11 07:52:35

无法检测以编程方式完成的更改。

但是,只要修改其值,您就可以触发 change 事件。

$("input[name=color]").val("newValue").change();

There is no way to detect the changes done programmatically.

However you can trigger a change event whenever you modify its value.

$("input[name=color]").val("newValue").change();
躲猫猫 2025-01-11 07:52:35

当通过 jquery 更改值时,您可以随后使用 jquery 触发更改事件

$("input[name=color]").val('someValue').trigger('change');

When change the value by jquery you can fire the change event with jquery afterwards

$("input[name=color]").val('someValue').trigger('change');
演出会有结束 2025-01-11 07:52:35

不,使用 javascript 更改值时不会触发 onchange 事件。或者,您可以在更改值时调用 onchange 事件。

No, onchange event will not be triggered when the value is changed using javascript. Alternatively you can call the onchange event when you change the value.

就是爱搞怪 2025-01-11 07:52:35

一种选择是跟踪侦听器范围之外的值

function foo () {
  var value = null;
  $("select#thingy").on("change", function (e) {
    let p = $(this).children("#choose-me").val();
    value = value === p ? null : p;
    $(this).val(value);
  });
}

$(function() {
  foo();
})

One option is to track the value outside the scope of your listener

function foo () {
  var value = null;
  $("select#thingy").on("change", function (e) {
    let p = $(this).children("#choose-me").val();
    value = value === p ? null : p;
    $(this).val(value);
  });
}

$(function() {
  foo();
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文