2015-11-23 74 views
2

這是我的第一個問題,所以我很抱歉,如果我做錯事。 我有一個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的。 謝謝你,前面!

+1

'localStorage'或'sessionStorage'可能會幫助你在這裏:) –

+0

Localstorage是一個好主意,就像@AhsN說的,或者你可以將你的英語和法語文本存儲在標記中的隱藏元素 –

+0

'localStorage'正在幫助,但我需要一個服務器端存儲,所以我可以在其他設備上訪問數據 – dmikic

回答

2

這裏有一個小提琴我做,它的工作原理,但如果它不完美,但我會很感激的反饋:

https://jsfiddle.net/szmnforr/

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; 
}; 

這實在是太醜陋,我不喜歡這個全局變量的使用,但它基本的工作原理。

+0

作品!非常感謝你! – dmikic

+0

我只能從sessionStorage的改變localStorage的,因此它可以是持久 – dmikic

+0

是不是在這一個點上接受?我認爲你應該在這裏接受其中的一個答案(不一定是我的答案),並在別處再問一次,否則對於任何未來的人會感到困惑。 – OliverRadini

0

我不知道,但我認爲這樣的事情應該做你的期望......

<select class="dropdown no-border" id="dropdownLanguage"> 
    <option selected="selected">Choose language</option> 
    <option value="0">English</option> 
    <option value="1">French</option> 
</select> 

<textarea name="agencyDescription" id="agencyDescription" class="about-agency" placeholder="Write something about your agency..." maxlength="5000">    
</textarea> 


<script type="text/javascript"> 

    var select = document.getElementById("dropdownLanguage"); 
    var selectArray = []; 
    var textArray = ["english txt","french txt"]; 
    var tArea = document.getElementById("agencyDescription"); 

    select.onclick = function(){ 
     textArray[parseInt(select.options[select.selectedIndex].value)]=tArea.value; 
    } 
    select.onchange = function(){ 
     tArea.value = textArray[parseInt(select.options[select.selectedIndex].value)]; 
    } 
</script> 
+0

以後能解釋一下,如果你想我必須現在完全移動 – goupil