有没有办法在 HTML 表单中多次跨越 ID?
如果我没有使用正确的术语,请提前原谅,我对编程的理解非常基础。但本质上我正在尝试用 HTML 和 Javascript 编写一个程序,该程序将根据用户输入自动在表单中填充 ID。我的问题是,当我需要在多个位置打印 ID 时,我只能在表单中打印一次 ID。这是突出显示我的问题的代码片段:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script language="JavaScript">
function showInput(){
document.getElementById('@userVLAN').innerHTML = document.getElementById("!userVLAN").value;
document.getElementById('@voiceVLAN').innerHTML = document.getElementById("!voiceVLAN").value;
document.getElementById('@mgmtVLAN').innerHTML = document.getElementById("!mgmtVLAN").value;
document.getElementById('@printerVLAN').innerHTML = document.getElementById("!printerVLAN").value;
}
</script>
<FONT FACE="COURIER NEW" SIZE="2">
<body>
<form>
<TABLE><TR>
<TD align="right" style="height:20px">VLAN TAGS:</TD>
<TD><input style="font-family:Courier New; font-size:12px;width:100px;height:15px" type="text" name="!userVLAN" placeholder="USER_VLAN ID" id="!userVLAN" oninput="userVLANfunction()"></TD>
<TD><input style="font-family:Courier New; font-size:12px;width:100px;height:15px" type="text" name="!voiceVLAN" placeholder="VOICE_VLAN ID" id="!voiceVLAN" oninput="voiceVLANfunction()"></TD>
<TD><input style="font-family:Courier New; font-size:12px;width:100px;height:15px" type="text" name="!mgmtVLAN" placeholder="MANAGEMENT_VLAN ID" id="!mgmtVLAN" oninput="mgmtVLANfunction()"></TD>
<TD><input style="font-family:Courier New; font-size:12px;width:100px;height:15px" type="text" name="!printerVLAN" placeholder="PRINTER_VLAN ID" id="!printerVLAN" oninput="printerVLANfunction()"></TD>
</TABLE>
</form>
<script>
function userVLANfunction() {var x = document.getElementById("!userVLAN").value;document.getElementById('@userVLAN').innerHTML = x;}
function voiceVLANfunction() {var x = document.getElementById("!voiceVLAN").value;document.getElementById('@voiceVLAN').innerHTML = x;}
function mgmtVLANfunction() {var x = document.getElementById("!mgmtVLAN").value;document.getElementById('@mgmtVLAN').innerHTML = x;}
function printerVLANfunction() {var x = document.getElementById("!printerVLAN").value;document.getElementById('@printerVLAN').innerHTML = x;}
</script>
vlan <span id='@userVLAN' style="background-color: #00FF00"></span><BR>
name USER_VLAN<BR>
vlan <span id='@voiceVLAN' style="background-color: #00FF00"></span><BR>
name VOICE_VLANx<BR>
vlan <span id='@mgmtVLAN' style="background-color: #00FF00"></span><BR>
name MANAGEMENT_VLAN<BR>
vlan <span id='@printerVLAN' style="background-color: #00FF00"></span><BR>
name PRINTER_VLAN<BR>
ip dhcp snooping vlan <span id='@userVLAN' style="background-color: #00FF00"></span>,<span id='@voiceVLAN' style="background-color: #00FF00"></span>,<span id='@mgmtVLAN' style="background-color: #00FF00"></span>,<span id='@printerVLAN' style="background-color: #00FF00"></span><BR>
ip arp inspection vlan <span id='@userVLAN' style="background-color: #00FF00"></span>,<span id='@voiceVLAN' style="background-color: #00FF00"></span>,<span id='@mgmtVLAN' style="background-color: #00FF00"></span>,<span id='@printerVLAN' style="background-color: #00FF00"></span><BR>
</FONT>
</body>
</html>
因此,当用户在文本框中输入 ID 时,它会填充 VLAN,但不会填充 dhcp 监听或 arp 检查。当前的解决方法是让用户将 VLAN 输入到多个文本框中,并为每个条目创建多个实例,但我更希望用户只需键入一次。谢谢。
Excuse me in advance if I am not using correct terminology, my understanding of programming is very basic. But essentially I am trying to write a program in HTML and Javascript that will automatically fill IDs in a form based on user input. My problem is that I can only print the ID once in the form, when I need it to print in multiple locations. Here is a snippet of the code that highlights my issue:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script language="JavaScript">
function showInput(){
document.getElementById('@userVLAN').innerHTML = document.getElementById("!userVLAN").value;
document.getElementById('@voiceVLAN').innerHTML = document.getElementById("!voiceVLAN").value;
document.getElementById('@mgmtVLAN').innerHTML = document.getElementById("!mgmtVLAN").value;
document.getElementById('@printerVLAN').innerHTML = document.getElementById("!printerVLAN").value;
}
</script>
<FONT FACE="COURIER NEW" SIZE="2">
<body>
<form>
<TABLE><TR>
<TD align="right" style="height:20px">VLAN TAGS:</TD>
<TD><input style="font-family:Courier New; font-size:12px;width:100px;height:15px" type="text" name="!userVLAN" placeholder="USER_VLAN ID" id="!userVLAN" oninput="userVLANfunction()"></TD>
<TD><input style="font-family:Courier New; font-size:12px;width:100px;height:15px" type="text" name="!voiceVLAN" placeholder="VOICE_VLAN ID" id="!voiceVLAN" oninput="voiceVLANfunction()"></TD>
<TD><input style="font-family:Courier New; font-size:12px;width:100px;height:15px" type="text" name="!mgmtVLAN" placeholder="MANAGEMENT_VLAN ID" id="!mgmtVLAN" oninput="mgmtVLANfunction()"></TD>
<TD><input style="font-family:Courier New; font-size:12px;width:100px;height:15px" type="text" name="!printerVLAN" placeholder="PRINTER_VLAN ID" id="!printerVLAN" oninput="printerVLANfunction()"></TD>
</TABLE>
</form>
<script>
function userVLANfunction() {var x = document.getElementById("!userVLAN").value;document.getElementById('@userVLAN').innerHTML = x;}
function voiceVLANfunction() {var x = document.getElementById("!voiceVLAN").value;document.getElementById('@voiceVLAN').innerHTML = x;}
function mgmtVLANfunction() {var x = document.getElementById("!mgmtVLAN").value;document.getElementById('@mgmtVLAN').innerHTML = x;}
function printerVLANfunction() {var x = document.getElementById("!printerVLAN").value;document.getElementById('@printerVLAN').innerHTML = x;}
</script>
vlan <span id='@userVLAN' style="background-color: #00FF00"></span><BR>
name USER_VLAN<BR>
vlan <span id='@voiceVLAN' style="background-color: #00FF00"></span><BR>
name VOICE_VLANx<BR>
vlan <span id='@mgmtVLAN' style="background-color: #00FF00"></span><BR>
name MANAGEMENT_VLAN<BR>
vlan <span id='@printerVLAN' style="background-color: #00FF00"></span><BR>
name PRINTER_VLAN<BR>
ip dhcp snooping vlan <span id='@userVLAN' style="background-color: #00FF00"></span>,<span id='@voiceVLAN' style="background-color: #00FF00"></span>,<span id='@mgmtVLAN' style="background-color: #00FF00"></span>,<span id='@printerVLAN' style="background-color: #00FF00"></span><BR>
ip arp inspection vlan <span id='@userVLAN' style="background-color: #00FF00"></span>,<span id='@voiceVLAN' style="background-color: #00FF00"></span>,<span id='@mgmtVLAN' style="background-color: #00FF00"></span>,<span id='@printerVLAN' style="background-color: #00FF00"></span><BR>
</FONT>
</body>
</html>
So when the user enters the IDs into the textbox it fills the vlan but not the dhcp snooping or arp inspection. The currently workaround is to have the user enter the vlans into multiple textboxes and creating severally instances of every entry, but I would prefer to have the user only have to type it once. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 html
class
因为 htmlid
必须是唯一的。然后您可以使用
for
来迭代它们。为了使事情变得更简单,您可以使用
querySelector()
和querySelectorAll()
按 ID 或类过滤 DOM 元素。这是根据您的代码片段编辑的示例:
You can use html
class
since htmlid
must be unique.Then you can use
for
to iterate through them.To make things easier, you can use
querySelector()
andquerySelectorAll()
to filter DOM element by their ID or class.Here's an example edited from your code snippet: