在Android中,如何使用Javascript设置WebView中编辑框的值 [英] In Android, how can I set the value of a edit box in WebView using Javascript

查看:90
本文介绍了在Android中,如何使用Javascript设置WebView中编辑框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解如何在WebView和使用Javascript的PC上的浏览器中设置编辑框的值。

I understand how to set the value of a edit box in WebView, and in a browser on PC with Javascript.

基本上是先找出编辑框的ID(文本),然后使用以下命令设置值:
在PC上:

It is basically first find out the ID of the edit box (text), then set the value using: On PC:

document.getElementById('xxxxxxxx').value = 'test'

在Android中:

mWebView.loadUrl("javascript: document.getElementById('xxxxxxxx').value = 'test'");

此脚本可在PC上运行,但是当我在WebView.loadUrl中调用该脚本时,它将覆盖我的整个页面进行测试。

This script works in PC, but when I call the script in WebView.loadUrl, it just override my entire page to "test".

这对我来说很奇怪,因为下面的android代码可以在单选按钮上工作,但是到编辑框时,它只会失败。

It is weird to me because the following android code works on the radio button, but when it comes to the edit box, it just fails.

mWebView.loadUrl("javascript: document.getElementById('xxxxxxxx').checked = true");

你们能帮我分析一下可能的原因吗?非常感谢。

Could you guys help me analyze what could be the possible reason? Thank you so much.

为了清楚起见,我在 WebViewClient.onPageFinished(WebView视图,字符串url)中运行javascript代码。因此,应该完全加载WebView。这也很有意义,因为我可以检查单选按钮。

To make it clear, I am running the javascript code in WebViewClient.onPageFinished(WebView view, String url). So the WebView should be fully loaded. That also makes sense because I can check the radio button.

推荐答案

问题已解决。这是因为我在Java代码中调用javascript的方式。

The problem is solved. It was because of the way I call javascript in java code.

类似于

mWebView.loadUrl("javascript: [your javascript code]");

有时可以工作,但并非在所有情况下都有效。至少在以下情况下,当我尝试在Web视图中设置文本框时,它不起作用:

sometimes work, but it doesn't work in all the case. At least it doesn't work for the following case when I tried to set the textbox in a webview:

mWebView.loadUrl("javascript: document.getElementById('xxxxxxxx').value= 'test'");

但如果您将javascript作为函数调用,它应该可以工作:

but it should work if you call the javascript as a function:

mWebView.loadUrl("javascript: (function() {document.getElementById('xxxxxxxx').value= 'test';}) ();" );

这解决了我的问题。希望对您有所帮助。

That fixed my problem. Hope it is helpful.

这篇关于在Android中,如何使用Javascript设置WebView中编辑框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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