谁能给我一个“技巧”如何将输入值传递给“a”元素onclick函数。假设我有具有特定 ID 的输入字段:<input id="yourname" type="text" value="Testing Value" class="form-control" type="text" placeholder="Your name here..." >和函数“exportText”,它实际上从特定的 div id(传记)导出文本(html):<a href="#" onclick="exportText('biography', 'Name of player');">
Save your biography
</a>当您单击 href 链接时,它会将传记保存在 txt 文档中,名称为:“播放器名称.txt”如何强制函数实际获取输入 id “yourname”值并使用该名称保存 .txt 文档,例如“yourname+.txt”我真的试图通过网络搜索解决方案,但我的知识太低,无法在自己身上找到答案。太感谢了!
2 回答

翻阅古今
TA贡献1780条经验 获得超5个赞
在您的 exportText 函数中,如果它是本地函数,您可以直接使用
document.getElementById('yourname').value;
而不是“玩家名称”参数现在,如果它是一个全局函数,您可以使用特定类(例如:ClsUserName)并在您的 exportText 中使用
document.getelementsbyclassname('ClsUserName')

梵蒂冈之花
TA贡献1900条经验 获得超5个赞
您可以使用事件:
input id="yourname" value="Testing Value" class="form-control" type="text" placeholder="Your name here..." />
<a href="#" id="exportText">Save your biography</a>
和js:
document.getElementById('exportText').addEventListener('click', function() {
console.log(document.getElementById('yourname').value)
})
添加回答
举报
0/150
提交
取消