In Android, how can I set the value of a edit box in WebView using Javascript

后端 未结 4 1518
醉酒成梦
醉酒成梦 2021-02-09 14:27

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

It is basically first find out the ID of the edit box (text), then s

4条回答
  •  無奈伤痛
    2021-02-09 15:09

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

    code like

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

    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'");
    

    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.

提交回复
热议问题