這是我的第一個問題,所以我很抱歉,如果我做錯事。 我有一個textarea
和一個選擇。我想更改textarea
內容,當我在select中更改選項時已輸入內容。但是,當我選擇我想要textarea
來顯示我以前輸入的內容時,回到之前的選項。textarea的內容變化的onchange在JavaScript
e.g當我選擇的選擇「英語」選項我想textarea
展現「英文文本」。然後,當我選擇「法國」我想textarea
,以示「法文本」,但是當我再次選擇「英語」選項,textarea
需要展現「英文文本」一次。
編輯:我需要在服務器端存儲數據,所以它可以從任何計算機上讀取!對不起,以前沒有定義它。
這裏是我的HTML代碼:
爲JS<select class="dropdown no-border" id="dropdownLanguage">
<option>Choose language</option>
<option>English</option>
<option>French</option>
</select>
<textarea name="agencyDescription" id="agencyDescription" class="about-agency" placeholder="Write something about your agency..." maxlength="5000">
<?php echo $agency->getData('agencyDescription'); ?>
</textarea>
@OliverRadini的代碼:
var languageDropdown = document.getElementById("dropdownLanguage");
var lastSelected = languageDropdown.options[languageDropdown.selectedIndex].value
console.log(lastSelected);
languageDropdown.onchange = function(){
var textBox = document.getElementById("agencyDescription");
currentText = textBox.value;
var selected = this.options[this.selectedIndex].value;
//load the current text into the last selected value
sessionStorage.setItem(lastSelected, currentText);
//load the new text in to the text box
textBox.value = sessionStorage.getItem(selected);
lastSelected = selected;
};
我想保持與JS,而不是jQuery的。 謝謝你,前面!
'localStorage'或'sessionStorage'可能會幫助你在這裏:) –
Localstorage是一個好主意,就像@AhsN說的,或者你可以將你的英語和法語文本存儲在標記中的隱藏元素 –
'localStorage'正在幫助,但我需要一個服務器端存儲,所以我可以在其他設備上訪問數據 – dmikic