服务器之家:专注于服务器技术及软件下载分享
分类导航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服务器之家 - 编程语言 - JavaScript - 用js传递value默认值的示例代码

用js传递value默认值的示例代码

2021-03-12 17:56JavaScript教程网 JavaScript

这篇文章主要介绍了用js传递value默认值的简单实现,很简单但比较实用,需要的朋友可以参考下

需求和代码如下:

“这是我的代码:”

?
1
2
<input type="text" id="price2" value="333"/>
<input type="text" id="trueprice" value="" />
?
1
2
3
4
5
<script type="text/javascript">
document.getElementById("price2").onkeyup = function() {
document.getElementById("trueprice").value = this.value;
}
</script>

问题:现在打开这个页面,trueprice的值默认是空的,怎样才能实现默认打开这个页面trueprice就已经和price2一样了呢?(price2是一个动态的数值)
<input type=”text” id=”trueprice” value=”” />是固定不可以修改的

我的一个简单实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE HTML>
 
<html>
 
<head>
 
<meta http-equiv="content-type" content="text/html;charset=utf-8">
 
<title>Test</title>
 
</head>
 
<body>
 
<input type="text" id="price2" value="333" onkeyup="test(this.value);"/>
 
<input type="text" id="trueprice" value="" />
 
<script type="text/javascript">
 
var price2 = document.getElementById("price2").value;
 
document.getElementById("trueprice").value = price2;
 
function test (defaultVal) {
 
document.getElementById("trueprice").value = defaultVal;
 
}
 
</script>
 
</body>
 
</html>

效果:

用js传递value默认值的示例代码
用js传递value默认值的示例代码
在第一个文本框中输入的内容能同步到第二个文本框

延伸 · 阅读

精彩推荐