javascript - 如何从函数 javascript 将值设置到接下来的输入中?

标签 javascript html

如何从函数 javascript 将值设置到下一个输入中?

测试此代码时,输​​入id="yyy"将得到value = 123没关系。

并输入id="xxx"将显示空白结果并显示错误。没关系。我明白了。

但是如果我想得到value = 123输入id="xxx"使用此代码(输入id="xxx"放在函数javascript的下一个位置)

我该怎么做?这可能吗?

<form>
    <input type="text" name="yy" id="yy"/>	
</form>	
		
<script>
    var test = "123";
	document.getElementById('yy').value = test;
	document.getElementById('xx').value = test;
</script>
		
<br>
<form>
    <input type="text" name="xx" id="xx"/>	
</form>

最佳答案

您正在加载元素之前加载脚本,这就是它给您未定义。将其放在元素后面即可解决您的问题

<form>
  <input type="text" name="yy" id="yy" />
</form>

<br>
<form>
  <input type="text" name="xx" id="xx" />
</form>
<script>
  var test = "123";
  document.getElementById('yy').value = test;
  document.getElementById('xx').value = test;
</script>

关于javascript - 如何从函数 javascript 将值设置到接下来的输入中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47776385/

相关文章:

javascript - 使用 jquery 加载动画以显示在特定的 div 中

html - html音频上没有鼠标响应(Windows App Ionic)

javascript - 检查复选框 :check is not working, 为什么,jquery?

javascript - 使用 Ajax 将数组传递给 PHP 脚本会导致空帖子

html - Bootstrap 断点在 chrome 检查器中不能正常工作?

html - CSS:SVG 旋转动画失败

html - 网站不滚动 |内容超出页面底部 |页脚出现在顶部

javascript - 在溢出容器中滑动 div,然后移除第一个元素并将其附加到末尾

javascript - 使用 typescript 扩展方法

javascript - Canvas : Getting rid of style attribute