我在 html 中有一個輸入,想將輸入的文本放入 JS 上的物件中并將其顯示在
結果在 html 上標記。因為不想簡化代碼,所以嘗試創建一個函式(這樣我可以為每個物件屬性呼叫它。
<input type="text" placeholder="Typ" id="type-mhouse" />
是輸入。
const land = {
Type: "",
Adress: "",
Value: "",
updateValue: function (x, y, z) {
this.x = document.getElementById(y).value;
document.getElementById(z).textContent = this.x;
console.log(this[0]);
},
};
是JS。
<p class="ergebnis" id="ergtype-house"></p>
是顯示結果的地方。
問題是:當我在輸入中輸入例如“Flat”時,它會在 html 輸出中正確顯示,但 land.Type 仍然是“”。
我已經嘗試了一切,但我對 JS 還是很陌生,所以我無法解決它。(我也知道我不應該將東西命名為“型別”或“值”......我會改變它。)
感謝您的任何評論!
重要編輯:土地物件用于鏈接的 js 檔案。該函式在單擊按鈕時在 HTML 內部呼叫:
<script>
$(document).ready(function () {
//Button on Modal
$("#submit").click(function () {
land.updateValue("Type", "type-mhouse", "ergtype-house");
});
});
</script>
uj5u.com熱心網友回復:
我不確定您想做什么,但這里有一個解決方案來解決您的問題。
const land = {
type: "",
address: "",
value: "",
updateValue: function(landAttributeName, textElementId, displayElementId) {
this[landAttributeName] = document.getElementById(textElementId).value;
document.getElementById(displayElementId).innerText = this[landAttributeName] || '';
},
};
順便說一句,你是對的,你應該使用更容易理解的名字:-)
計算機科學中只有兩件難事:快取失效和命名。——菲爾·卡爾頓
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/342943.html
標籤:javascript 功能 目的 函数参数
