首頁  >  文章  >  web前端  >  用js傳遞value預設值的範例程式碼_javascript技巧

用js傳遞value預設值的範例程式碼_javascript技巧

WBOY
WBOY原創
2016-05-16 16:36:31990瀏覽

需求與程式碼如下:

「這是我的程式碼:」

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

問題:現在打開這個頁面,trueprice的值預設是空的,怎麼才能實現預設打開這個頁面trueprice就已經和price2一樣了呢? (price2是一個動態的數值)
是固定不可以修改的

我的一個簡單實作:

<!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>

效果:



在第一個文字方塊中輸入的內容能同步到第二個文字方塊

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn