簡體   English   中英

如何將標簽中的值分配給輸入?

[英]How do I assign the value in the label to the input?

我從js獲得此值,並且可以將其分配給標簽,如何將標簽中的值分配給輸入? 還是我如何從js獲取此值以分配給輸入?

 var height = 150; document.getElementById("heightLabel").innerHTML = height; 
 <div class="form-group"> <label id="heightLabel"> height </label> </div> <div class="form-group"> <input class="form-control" type="number" id="heightLabel" name="heightLabel" value=""> </div> 

幾件事情:

  • id必須是唯一的。 您無法為頁面上的多個元素提供相同的id值。 如果兩者都需要,則應使用一個class (但這不適用於上面的示例)。
  • 要為<input/>元素的值賦值,您將需要使用.value (或可替代地使用.setAttribute('value', height) )。

 // let height = 150; (To create in JS) // To get label element value let height = document.getElementById("heightLabel").innerText; // Ensure it is a number height = Number(height); if (isNaN(height)) { height = 0; } // Set the input value document.getElementById("heightValue").value = height; 
 <div class="form-group"> <label id="heightLabel"> 999 </label> </div> <div class="form-group"> <input class="form-control" type="number" id="heightValue" name="heightLabel" value=""> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM