我的 html 正文中有 10 个表单。所有形式都是用不同的形式创建的。示例
<form method="post" id="formid1" name="abc">
<input type="button" value="xyz" style="height: 115px; width: 300px;">
</form>
.
.
.
<form method="post" id="formid10" name="abc10">
<input type="button" value="xyz10" style="height: 115px; width: 300px;">
</form>
我想在表中打印表单名称(abc、abc1.... abc10)。我怎样才能实现这一目标?
我尝试使用 JavaScript 函数创建一个按钮:
<div>
<button id="hide1" onclick="myFunction()"Service </button>
</div>
<script>
function myFunction() {
var x = document.getElementById("formid1").name;
document.getElementById("demomain").innerHTML = "The name of the form is: " + x;
}
</script>
但我无法在表格内打印它。请帮忙
最佳答案
你可以这样做:
var names = null;
var forms = document.getElementsByTagName('form');
for (var i = 0; i <= forms.length; i++) {
names += "The name of the form is: " + forms[i].getAttribute('name');
document.getElementById("demomain").innerHTML = names;
}
See DEMO
关于javascript - 在表内打印表单 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25543045/