使用innerHTML和< input> [英] using innerHTML with <input>

查看:79
本文介绍了使用innerHTML和< input>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在输入标记上使用innerHTML方法,而我得到的所有内容都是空白字符串。这是我正在使用的代码。

I am trying to use the innerHTML method on an input tag and all i get back is a blank string. Here is the code i am useing.

javascript

function setName(ID){
    document.getElementById('searchtitle').innerHTML = "Enter " + ID.innerHTML;
}

HTML

<input type="radio" name="searchtype" id="test" value="name" onclick="setName(this)">Last Name</input><br/>
<input type="radio" name="searchtype" value="phonenumber" onclick="setName(this)">Phone Number</input><br/>

<label for="inputfield" id="searchtitle" style="font-size:2em;">Enter Last Name</label><br/>
<input type="text" name="inputfield" id="inputfield" style="font-size:2em;"></input>

应该发生什么取决于我选择哪个单选按钮输入框的标签应该改变。我可以制作label.innerHTML = radio.value,但这些值是以我的php代码命名的,并且没有很好地格式化(即.phonenumber与电话号码)这就是为什么我试图使用单选按钮的innerHTML。

What is supposed to happen is depending on which radio button I pick the label for the input box should change. I can make the label.innerHTML=radio.value but the values are named for my php code and not formated nicely(ie. phonenumber vs. Phone Number) this is why I am trying to use the innerHTML of the radio button.

我能得到的任何帮助都会受到极大关注。

Any help I could get would be greatly appriciated.

推荐答案

你应该在标签标记内嵌入输入。输入标记应由 /> 关闭。它是语义HTML。执行此操作时,单击标签激活输入。 InnerHTML仅适用于标签。它将返回您的标签值。

you should embed input inside of label tag. input tag should closed by />. It's semantic HTML. When you do this clicking on label activate the input. InnerHTML only works for label then. It will return you label value.

<label for="inputfield" id="searchtitle" style="font-size:2em;">Enter Last Name
    <input type="text" name="inputfield" id="inputfield" style="font-size:2em;" />
</label>

JavaScript:

JavaScript:

console.log(document.getElementById('searchtitle').innerHTML); // returns 'Enter Last Name'

这篇关于使用innerHTML和&lt; input&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆