Android 使用javascript函数设置HTML文本框值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11770858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting HTML textbox value using javascript function?
提问by user1570222
I'm using this code to set the HTML textbox value using Javascript function. But it seems to be not working. Can anyone point out, what is wrong with this code?
我正在使用此代码使用 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>
the "value" is came from my android java class using this codes
“值”来自使用此代码的我的 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 +")");
回答by Hazem Salama
function setValue(value) {
var myValue=value; //unnecessary
document.getElementById("name").value= myValue;
}
But then as pointed out in the comments, you need to call setValue(value) somewhere in your code. Right now you just defined the function is never called.
但是正如评论中指出的那样,您需要在代码中的某处调用 setValue(value) 。现在你刚刚定义的函数永远不会被调用。
回答by Venkatesh S
You could either access the element's value by its name:
您可以通过名称访问元素的值:
document.getElementsByName("textbox1"); // returns a list of elements with name="textbox1"
document.getElementsByName("textbox1")[0] // returns the first element in DOM with name="textbox1"
So:
所以:
input name="buttonExecute" onclick="execute(document.getElementsByName('textbox1')[0].value)" type="button" value="Execute" />
Or you assign an ID to the element that then identifies it and you can access it with getElementById:
或者您为元素分配一个 ID,然后标识它,您可以使用 getElementById 访问它:
<input name="textbox1" id="textbox1" type="text" />
<input name="buttonExecute" onclick="execute(document.getElementById('textbox1').value)" type="button" value="Execute" />
回答by Mahendra Liya
Firstly, you have a typo in your javascript function i.e. you have used getElementsById
as compared to getElementById
首先,您的 javascript 函数中有一个错字,即您使用getElementsById
的与getElementById
To set the value of the textbox on page load, I suggest you use an alternative
要在页面加载时设置文本框的值,我建议您使用替代方法
<body onload="setValue('yourValueToSet');">
<!-- Your usual html code in the html file -->
</body>
回答by code-jaff
I think you are missing the quotes,
我认为你缺少引号,
try,
尝试,
web.loadUrl("javascript:setValue('"+ value +"')");
also consider about the typo.
还要考虑打字错误。
回答by Roger
You are not linking the function to anything. For example, a click:
您没有将功能链接到任何东西。例如,单击:
<input id="name" value="" onclick="javascript:this.value=12;"/>
Replace the onclick attribute for your desired function, whatever it does (you need to be more specific)
替换您所需功能的 onclick 属性,无论它做什么(您需要更具体)
Also, there is no language attribute (at least not anymore) use type="text/javascript"
instead
此外,没有语言属性(至少不再)使用type="text/javascript"
代替
Here is a fiddle: http://jsfiddle.net/4juEp/
这是一个小提琴:http: //jsfiddle.net/4juEp/
Click the input to see it working.
单击输入以查看其工作情况。
Look at this second fiddle. http://jsfiddle.net/4juEp/1/
看看这第二把小提琴。http://jsfiddle.net/4juEp/1/
which loads whatever is defined in the hid input to the name input.
它将 hid 输入中定义的任何内容加载到名称输入。
回答by Ahsan Khurshid
You are using document.getElementsById("name")
it should be document.getElementById("name")
你正在使用document.getElementsById("name")
它应该是document.getElementById("name")
not Elements
it is Element
没有Elements
它Element
回答by Sagar V
Check this out:
看一下这个:
<body onload="setvalue($value);">
Whats your Name?<input id="name" name="name" value=""/>
<script type="text/javascript">
function setValue(value){
document.{formname}.name.value = value;}</script>
回答by user3451875
It's not Elements It's Element
这不是元素 这是元素
You should use document.getElementById('object-id');
你应该使用 document.getElementById('object-id');