简体   繁体   English

使用javascript函数设置HTML文本框值?

[英]Setting HTML textbox value using javascript function?

I'm using this code to set the HTML textbox value using Javascript function. 我正在使用此代码使用Javascript函数设置HTML文本框值。 But it seems to be not working. 但似乎没有用。 Can anyone point out, what is wrong with this code? 任何人都可以指出,这段代码有什么问题?

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 “value”来自我的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 +")");
   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. 但是,正如评论中指出的那样,您需要在代码中的某处调用setValue(value)。 Right now you just defined the function is never called. 现在你刚刚定义了函数永远不会被调用。

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" />

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输入中定义的任何内容加载到名称输入。

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

Firstly, you have a typo in your javascript function ie 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>

I think you are missing the quotes, 我想你错过了报价,

try, 尝试,

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

also consider about the typo. 还要考虑一下错字。

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>

It's not Elements It's Element 它不是元素元素

You should use document.getElementById('object-id'); 你应该使用document.getElementById('object-id');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM