javascript怎么改变input value值

javascriptjavascript 2023-08-29 14:28:05 696
摘要: javascript改变inputvalue值的方法方法1:利用value属性<!DOCTYPEhtml><html>        <head>                <metacharset="utf-8">        </head>        <body>                <inputid="inp"value="默认值"/>                <br/><br/>                <buttononclick="...

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>

1.gif

方法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>

2.gif

【相关推荐:javascript学习教程