将图像 alt 属性值作为表单字段值

Make an image alt attribute value as a form field value

我想将图像的 alt 值添加为表单字段值。如何使用 JS 或 Jquery?

谢谢

<img src="x.jpg" alt="grab me" id="ok" />

<form action="/#wpcf7-f1352-o1" method="post" class="wpcf7-form init" novalidate="novalidate" data-status="init">
  
<span class="image-alt"><input type="text" name="iamge-alt" value="" size="40"  aria-invalid="false"></span>

<input type="submit" value="OK" class="wpcf7-submit">
</form>

.getAttribute()alt属性然后.forms[0]第一个<form>.elements[0]第一个表单控件(<input>) 到 .value 属性.

const alt = document.querySelector('img').getAttribute('alt');

document.forms[0].elements[0].value = alt;
<img src="x.jpg" alt="grab me" id="ok" />

<form action="/#wpcf7-f1352-o1" method="post" class="wpcf7-form init" novalidate="novalidate" data-status="init">
  
<span class="image-alt"><input type="text" name="iamge-alt" value="" size="40"  aria-invalid="false"></span>

<input type="submit" value="OK" class="wpcf7-submit">
</form>