【发布时间】:2021-12-24 21:48:05
【问题描述】:
我需要将输入字段中的值复制到剪贴板。我没有得到输入字段的值,而是得到了 null。
这是我的代码:
<input type="text" value=${model.anchor_url} id=${properties.anchor_name}>
<button class="c-button" onclick="myFunction()">
<span class="c-button__label">Copy</span>
</button>
<div data-sly-test="${model.anchor_url}">
<p>URL from Model :</p>
<pre>${model.anchor_url}</pre>
</div>
function myFunction() {
const field = document.getElementById(`${properties.anchor_name}`);
navigator.clipboard.writeText(field);
alert("Copied the text to clipboard: " + field);
}
field 变量的值结果为 null,即使 id 存在并且它有一个值。
【问题讨论】:
-
properties.anchor_name的值是多少?看起来您使用字符串${properties.anchor_name}作为输入的 ID,而不是变量的值。 -
properties.anchor_name 是一个变量,它有一个值,例如。锚 1。我不确定这是否是您所要求的。
-
我不确定我是否理解您的建议@Lain,因为 ${properties.anchor_name} 是一个字符串值。
-
@Anuska 简单来说,他说你不需要把
$[}
标签: javascript html