javascript怎么改变input value值

javascript怎么改变input value值,第1张

javascript怎么改变input value值

改变方法:1、利用value属性,语法“input对象.value = "修改后的值";”;2、利用setAttribute()方法,语法“input对象.setAttribute("value","修改后的值");”。

本教程 *** 作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

javascript改变input value值的方法

方法1:利用value属性

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<input id="inp" value="默认值" />
		<br /><br />
		<button onclick="myFunction()">修改input的值</button>
		<script>
			function myFunction() {
				var input = document.getElementById("inp");

				input.value = "修改后的值";
			}
		</script>
	</body>

</html>

方法2:利用setAttribute()方法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<input id="inp" value="默认值" />
		<br /><br />
		<button onclick="myFunction()">修改input的值</button>
		<script>
			function myFunction() {
				var input = document.getElementById("inp");

				input.setAttribute("value","修改后的值");
			}
		</script>
	</body>

</html>

【相关推荐:javascript学习教程

以上就是javascript怎么改变input value值的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/691201.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-21
下一篇2022-04-21

发表评论

登录后才能评论

评论列表(0条)

    保存