目前,如果我重繪 頁面,我會隨機顯示 4 位數字。我嘗試在 4 位數字前面添加年份,但仍然顯示 4 位數字。反正有沒有顯示亂數前面的年份?(例如,20221234)。
<label class="control-label col-sm-4" for="refno">REF nos :</label>
<div class="col-sm-4">
<script type="text/javascript">
now = new Date();
randomNum = '';
randomNum = Math.round(Math.random() * 9);
randomNum = Math.round(Math.random() * 9);
randomNum = now.getTime().toString().slice(-2);
window.onload = function () {
document.getElementById("refno").value = randomNum;
}
document.getElementById("refno").innerHTML = new Date().getFullYear();
</script>
<p class="form-control-static" style="margin-top: -6px;">
<input type="text" class="form-control" id="refno" name="refno" value="<?php echo $refno; ?>" disabled>
</p>
</div>
<div class="col-sm-10"></div>
uj5u.com熱心網友回復:
您不能innerHTML
在input
標簽上使用。
您可以使用document.getElementById('someID').value
來替換該值。
const now = new Date();
let randomNum = '';
randomNum = Math.round(Math.random() * 9);
randomNum = Math.round(Math.random() * 9);
randomNum = now.getTime().toString().slice(-2);
document.getElementById("refno").value = `${new Date().getFullYear()}${randomNum}`;
<label class="control-label col-sm-4" for="refno">REF nos :</label>
<div class="col-sm-4">
<br/>
<p class="form-control-static" style="margin-top: -6px;">
<input type="text" class="form-control" id="refno" name="refno" value="" disabled />
</p>
</div>
<div class="col-sm-10"></div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/504473.html
標籤:javascript html
上一篇:僅更改變數的名稱似乎會破壞JavaScript代碼以將鏈接復制到剪貼板
下一篇:字串、向量和陣列