3

このコードを使用して、Javascript 関数を使用して HTML テキストボックスの値を設定しています。しかし、それは機能していないようです。このコードの何が問題なのか、誰でも指摘できますか?

Whats your Name?
<input id="name" value="" />

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

「値」は、このコードを使用して Android Java クラスから取得されます

String value = "Isiah";
    WebView web = (WebView) findViewById(R.id.web1);
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("file:///android_asset/www/webpage");
    web.loadUrl("javascript:setValue("+ value +")");
4

8 に答える 8

7
   function setValue(value) {
    var myValue=value; //unnecessary
    document.getElementById("name").value= myValue;
}

ただし、コメントで指摘されているように、コードのどこかで setValue(value) を呼び出す必要があります。今、関数が呼び出されないように定義したところです。

于 2012-08-02T04:14:20.913 に答える
1

要素の値には、その名前でアクセスできます。

  document.getElementsByName("textbox1"); // returns a list of elements with name="textbox1"
    document.getElementsByName("textbox1")[0] // returns the first element in DOM with name="textbox1"

それで:

    input name="buttonExecute" onclick="execute(document.getElementsByName('textbox1')[0].value)" type="button" value="Execute" />

または、要素にIDを割り当てて、それを識別し、getElementByIdを使用してアクセスできます。

    <input name="textbox1" id="textbox1" type="text" />
    <input name="buttonExecute" onclick="execute(document.getElementById('textbox1').value)" type="button" value="Execute" />
于 2012-08-02T04:22:48.327 に答える
0

私はあなたが引用符を逃していると思います、

試す、

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

タイプミスについても検討してください。

于 2012-08-02T04:45:38.217 に答える
0

あなたはそれを使用しdocument.getElementsById("name")ているはずですdocument.getElementById("name")

ElementsそうではないElement

于 2012-08-02T04:14:29.010 に答える
0

まず、javascript 関数にタイプミスがありますgetElementsByIdgetElementById

ページの読み込み時にテキストボックスの値を設定するには、別の方法を使用することをお勧めします

<body onload="setValue('yourValueToSet');">

<!-- Your usual html code in the html file -->

</body>
于 2012-08-02T04:36:08.483 に答える
0

関数を何にもリンクしていません。たとえば、次のようにクリックします。

<input id="name" value="" onclick="javascript:this.value=12;"/>

目的の関数の onclick 属性を置き換えます (より具体的にする必要があります)。

type="text/javascript"また、代わりに使用する言語属性はありません(少なくとももうありません)

ここにフィドルがあります:http://jsfiddle.net/4juEp/

入力をクリックして、動作を確認します。

この 2 番目のフィドルを見てください。http://jsfiddle.net/4juEp/1/

これは、hid 入力で定義されているものを name 入力にロードします。

于 2012-08-02T04:10:43.630 に答える