2

我正在使用此代码,但值表示“未定义”有人能指出我的问题吗?

这是我的java类代码

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.frux.web.R.layout.activity_main);
    String value = "Isiah";
    WebView web = (WebView) findViewById(R.id.web1);
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("file:///android_asset/www/index.html");
    web.loadUrl("javascript:setValue(\""+ value +"\")");


}

这是我的网页代码

<!DOCTYPE html>
<html>
<head>
</head>
<body>

Whats your Name?
<input id="name" value="" />
<button onclick = "setValue()">Submit</button>

<script type="text/javascript"> 
function setValue(value){
var myValue = value;
document.getElementById("name").value = myValue;

}
</script>
</body>
</html>

我也试试这个

web.loadUrl("javascript:setValue('"+ value +"')");

任何想法将不胜感激

我使用了这个 HTML 代码中的代码,它的显示与我发布的上面的其他代码没有什么不同。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>


<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">  

function setValue(amount1)
{
    myValue = amount1;
    document.getElementById("amount").value = myValue;
  }

function rand ( n )
{
    document.getElementById("orderRefId").value =  ( Math.floor ( Math.random ( ) * n + 1 ) );
}



</script>
</head>
<body onLoad="rand(200000);setValue();">
        <!-- 
            Note: https://www.pesopay.com/b2c2/eng/payment/payForm.jsp for live payment URL
                  https://test.pesopay.com/b2cDemo/eng/payment/payForm.jsp for test payment URL
        -->
        <form method="POST" name="frmPayment" action="https://test.pesopay.com/b2cDemo/eng/payment/payForm.jsp">
        <table>
        <tbody>
        <tr>
            <td>Order Reference No. (your reference number for every transaction that has transpired):</td> 
            <td><input type="text" id="orderRefId" name="orderRef" value="Test-001"/></td>
        </tr>   
        <tr>
            <td>Amount:</td>
            <td><input type="text" name="amount" id="amount" value=""/></td>

        </tr>
        <tr>
            <td>Currency Code - "608" for Philippine Peso, "840" for US Dollar:</td>
            <td><input type="text" name="currCode" value="608"/></td>
        </tr>   
        <tr>
            <td>Language:</td>
            <td><input type="text" name="lang" value="E"/></td>
        </tr>   
        <tr>
            <td>Merchant ID (the merchant identification number that was issued to you - merchant IDs between test account and live account are not the same):</td> 
            <td><input type="text" name="merchantId" value="18056869"/></td>
        </tr>
        <tr>    
            <td>Redirect to a URL upon failed transaction:</td>
            <td><input type="text" name="failUrl" value="http://www.yahoo.com?flag=failed"/></td>
        </tr>   
        <tr>
            <td>Redirect to a URL upon successful transaction:</td>
            <td><input type="text" name="successUrl" value="http://www.google.com?flag=success"/></td>
        </tr>
        <tr>
            <td>Redirect to a URL upon canceled transaction:</td>
            <td><input type="text" name="cancelUrl" value="http://www.altavista.com?flag=cancel"/></td>
        </tr>
        <tr>
            <td>Type of payment (normal sales or authorized i.e. hold payment):</td> 
            <td><input type="text" name="payType" value="N"/></td>
        </tr>
        <tr>    
            <td>Payment Method - Change to "ALL" for all the activated payment methods in the account, Change to "BancNet" for BancNet debit card payments only, Change to "GCASH" for GCash mobile payments only, Change to "CC" for credit card payments only:</td>
            <td><input type="text" name="payMethod" value="ALL"/></td>
        </tr>
        <tr>    
            <td>Remark:</td>
            <td><input type="text" name="remark" value="Asiapay Test"/></td>
        </tr>
        <!--<tr>    
            <td>Redirect:</td>
            <td><input type="text" name="redirect" value="1"/></td>
        </tr>-->
        <tr>
            <td></td>
        </tr>   

        <input type="submit" value="Submit">




        </tbody>
        </table>    


        </form>

</body>
</html>             
4

4 回答 4

1

将传递的值用双引号括起来:

web.loadUrl("javascript:setValue(\""+value+"\")");

我懂了!当您loadUrl第二次调用时,页面尚未加载。解决方案是将您的setValue呼叫附加到window.onload事件:

super.loadUrl("javascript:window.onload = function(){setValue(\"haha\");};");

此代码将“哈哈”正确加载到输入中。

于 2012-08-02T09:45:13.083 回答
0

尝试将您的价值用单引号括起来,像这样

 web.loadUrl("javascript:setValue('"+ value +"')");
于 2012-08-02T09:40:03.950 回答
0

我在准备好之前尝试访问 DOM 时遇到了这个问题。devmilles.com 的解决方案有效,但我花了更多时间进行试验。

两者的

webView.loadUrl("javascript:window.addEventListener('DOMContentLoaded',
    function(){setValue('haha');}, false);");

webView.loadUrl("javascript:window.addEventListener('load',
    function(){setValue('haha');}, false);");

为我工作,但将事件附加到document而不是window没有。DOMContentLoaded,在支持的情况下,响应速度略快于load

此外,事实证明WebViewClient总是有一个onPageFinished()事件,它似乎像load事件一样工作

webView.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:setValue('haha');");
    }
});

最后,我可以模仿这种行为

webView.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
        if (progress == 100) {
            view.loadUrl("javascript:setValue('haha');");
        }
    }
});

但我认为没有理由在这种WebViewClient方法上使用它(它们可能等效也可能不等效)。

于 2014-12-18T11:08:14.953 回答
0

我也面临同样的问题。但是使用以下方式解决了问题。

     String testValue="abcdedfg";
    String value = "\'"+testValue+"\'";
    web.loadUrl("javascript:setValue(\""+value+"\")");
于 2016-04-14T05:47:00.263 回答