在 Firefox 中执行对 field.onchange 的引用

发布于 2024-09-16 01:21:12 字数 780 浏览 8 评论 0原文

我有 HTML 代码,它从表单调用 javascript 函数,使用:

<form name="f" id="f" ...>
  <input name="myField" onchange="doFunct(f.myField.value,f.yourField);" />
  <input name="yourfield" onchange="doFunct(f.yourField.value,f.anotherField);" />
...

在 javascript 代码中:

function doFunct(field,dest){
    // do something with field
    dest.value = field;
    // see if the dest has a change field
    if (dest.onchange !== null) {
        //we have an onchange function, so let's do it!
        dest.onchange();
    }
}

这在 Safari、Opera 和 Chrome 中运行良好。它在 FireFox 中失败并出现错误:

错误:dest.onchange 不是函数

关于如何在 FireFox 中从 javascript 执行“dest.onchange()”有什么建议吗?

我需要这种功能来级联对输入表单中字段的更改。

I have HTML code which calls a javascript function from a form, using:

<form name="f" id="f" ...>
  <input name="myField" onchange="doFunct(f.myField.value,f.yourField);" />
  <input name="yourfield" onchange="doFunct(f.yourField.value,f.anotherField);" />
...

In the javascript code:

function doFunct(field,dest){
    // do something with field
    dest.value = field;
    // see if the dest has a change field
    if (dest.onchange !== null) {
        //we have an onchange function, so let's do it!
        dest.onchange();
    }
}

This works fine in Safari, Opera, and Chrome. It fails in FireFox with an error:

Error: dest.onchange is not a function

Any suggestions on how to execute "dest.onchange()" from javascript in FireFox?

I need this capability to cascade changes to fields in an input form.

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

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

发布评论

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

评论(2

稀香 2024-09-23 01:21:12

要执行事件,请勿添加“on”前缀,只需运行 dest.change();

To execute events, do not add the 'on' prefix to it, just run dest.change();

经过更多研究,在我的 O'Reilly JavaScript Pocket Reference 中,我发现有一个段落指出:

null和 undefined

JavaScript 关键字 null< /code> 是一个特殊值,表示“无值”。如果变量包含 null,您就知道它不包含任何类型的有效值。 JavaScript 中还有另一个特殊值:未定义值。这是当您使用未声明或未初始化的变量或使用不存在的对象属性时返回的值。 此值没有 JavaScript 关键字。

在使用alert(dest.onchange)进行一些测试后,我发现 Firefox 并没有抱怨每次调用dest.onchange(),而是仅那些错误未定义)。显然(Grrrr!)Firefox 没有收到备忘录:[此值没有 JavaScript 关键字。]

如果我更改代码来测试 dest.onchange !== undefined< /code> 然后 Firefox 很满意,但 Safari、Opera 和 Chrome 在测试中失败了。如果我按如下方式更改代码,它就可以在所有四种浏览器中运行。

if ( (dest.onchange !== null)                // test for safari, chrome, opera
     && (dest.onchange !== undefined) ) {    // test for firefox
    //we have an onchange function, so let's do it!
    dest.onchange();
}

我花了 8 个小时试图找出 Firefox 运行不佳的原因。

It appears from more research, in my O'Reilly JavaScript Pocket Reference, I find there's a paragraph that states:

null (and undefined)

The JavaScript keyword null is a special value that indicates "no value". If a variable contains null, you know that it does not contain a valid value of any type. There is one other special value in JavaScript: the undefined value. This is the value returned when you use an undeclared or uninitialized variable or when you use a non-existent object property. There is no JavaScript keyword for this value.

After some testing with alert(dest.onchange) I found that Firefox was not complaining about every invocation of dest.onchange() but only those which were errors (undefined). Apparently (Grrrr!) Firefox didn't get the memo: [There is no JavaScript keyword for this value.]

If I change my code to test for dest.onchange !== undefined then Firefox is happy, but then Safari, Opera and Chrome FAIL at the test. If I change the code as below, it works in all four browsers.

if ( (dest.onchange !== null)                // test for safari, chrome, opera
     && (dest.onchange !== undefined) ) {    // test for firefox
    //we have an onchange function, so let's do it!
    dest.onchange();
}

And I got to spend 8 hours trying to figure out why Firefox doesn't play nice.

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