使用 JSObject 从 Java 调用 Javascript 函数

发布于 2024-09-17 18:40:28 字数 511 浏览 4 评论 0原文

我只是想使用 JSObject 从 Java 调用 Javascript 函数。我需要 Javascript 函数来更新 HTML 文本字段。为此,我需要知道元素 ID 和值。

我已经尝试过这个,但它不起作用。当尝试从 Javascript 访问时,传递的值为空。

所以我的问题是如何从 Javascript 访问这些值?我一定是错过了什么。

如果我声明大小为 1 的对象,它会起作用,但我需要在对象数组中传递 2 个值。

提前致谢。

//Java code
Object[] objects = new Object[2];
objects[0] = "elementId";
objects[1] = "elementValue";
window.call("updateElement",objects);

//Javascript code
function updateElement(array){
  alert(array[0]);
  alert(array[1]);
}

I'm just trying to call a Javascript function from Java using JSObject. I need the Javascript function to update an HTML text field. For this I need to know the element ID and the value.

I have tried this but it doesn't work. When trying to access from Javascript the values passed are empty.

So my question is how can I access those values from Javascript? I must be missing something.

If I declare objects of size 1 it will work, but I need to pass 2 values in objects array.

Thanks in advance.

//Java code
Object[] objects = new Object[2];
objects[0] = "elementId";
objects[1] = "elementValue";
window.call("updateElement",objects);

//Javascript code
function updateElement(array){
  alert(array[0]);
  alert(array[1]);
}

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

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

发布评论

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

评论(3

云淡月浅 2024-09-24 18:40:28

所以我注意到您在 java 中使用对象数组并传入字符串值。
您是否尝试过使用字符串数组?

String[] x = new String[2];
x[0] = "elementId";
x[1] = "elementValue";
window.call("updateElement", x);

尝试一下,看看是否有效。我的猜测是,javascript 无法意识到传递的对象数组是字符串数组。虽然我不是100%确定。

So I noticed that you are using an Object array in java and passing in string values.
Have you tried using a String array?

String[] x = new String[2];
x[0] = "elementId";
x[1] = "elementValue";
window.call("updateElement", x);

give that a try and see if that works. my guess is that javascript isnt able to realize that object array being passed is a string array. though i am not 100% sure.

静若繁花 2024-09-24 18:40:28

在您的示例中,您应该看到 alert('e')alert('l') 显示,因为您将两个参数传递给 'updateElement'。您始终可以使用 javascript 中的 arguments 对象来检查您真正收到的内容。

我建议始终单独并最后创建最终参数列表,这样就不会出现此问题。他们没有将 Java 5 中的 JSObject.call(String, Object...) 签名更新为一个可怕的设计缺陷,这样我们就不会那么头疼了。

一个更干净的解决方案是:

// Java code
window.call("updateElement", new Object[] { "elementId", "elementValue" });

// Javascript code
function updateElement(elId, elVal){
    alert(elId);
    alert(elVal);
}

但如果你真的想坚持在 JS 中接收数组:

String[] arr = new String[] { "elementId", "elementValue" };
window.call("updateElement", new Object[] { arr });

In your example you should see alert('e') and alert('l') show up because you're passing two arguments to 'updateElement'. You can always use the arguments object in javascript to inspect what you really received.

I suggest to always create the final argument list separately and last, so you don't have this problem. It's just a horrible design flaw that they didn't update the signature to JSObject.call(String, Object...) in Java 5, we would have much less headaches that way.

A cleaner solution would be:

// Java code
window.call("updateElement", new Object[] { "elementId", "elementValue" });

// Javascript code
function updateElement(elId, elVal){
    alert(elId);
    alert(elVal);
}

but if you really want to stick to receiving an array in JS:

String[] arr = new String[] { "elementId", "elementValue" };
window.call("updateElement", new Object[] { arr });
梦过后 2024-09-24 18:40:28

调用的工作方式是传入函数参数数组。这里你只有一个参数(你的数组),所以你需要像这样调用它:

//Java code
Object[][] objects = new Object[][1];
objects[0]=new Object[2];
objects[0][0] = "elementId";
objects[0][1] = "elementValue";
window.call("updateElement",objects);

The way call works is that you pass in an array of the functions arguments. Here you have only one argument ( your array), so you need to call it like :

//Java code
Object[][] objects = new Object[][1];
objects[0]=new Object[2];
objects[0][0] = "elementId";
objects[0][1] = "elementValue";
window.call("updateElement",objects);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文