2013-03-10 60 views
0

基本佈局:更新1個文本區域,經過多個環節

<textarea id="courseText"></textarea> 

<ul class="chapter-items"> 

    <li id="chap1link">Chapter 1</li> 
    <input id="chap1value" type="hidden" value="I am he as you are he as you are me and we are all together" /> 

    <li id="chap2link">Chapter 2</li> 
    <input id="chap2value" type="hidden" value="See how they run like pigs from a gun, see how they fly" /> 

    <li id="chap3link">Chapter 3</li> 
    <input id="chap3value" type="hidden" value="I'm crying" /> 

</ul> 

是否有可能更新textarea的courseText每當點擊與值與相應隱藏字段

如果單擊了chap1link li,它會將chap1value的值加載到textarea上。

任何意見,將不勝感激。謝謝!

+0

是的,這是可能用JavaScript。您可以將一個點擊事件附加到'li',並在該函數內設置'textarea'的值。 – Aiias 2013-03-10 07:43:22

回答

3

嘗試:

$(".chapter-items li").click(function(){  
    $("#courseText").val($(this).next('input[type=hidden]').val()); 
}); 

Sample

不過我建議以下HTML標記(一ul裏的任何東西必須是在li

<ul class="chapter-items"> 
    <li id="chap1link">Chapter 1 
    <input id="chap1value" type="hidden" value="I am he as you are he as you are me and we are all together" />  
    </li> 
    <li id="chap2link">Chapter 2 
    <input id="chap2value" type="hidden" value="See how they run like pigs from a gun, see how they fly" /> 
    </li> 
    <li id="chap3link">Chapter 3 
    <input id="chap3value" type="hidden" value="I'm crying" /> 
    </li> 
</ul> 

而遵循js代碼:

$(".chapter-items li").click(function(){  
    $("#courseText").val($(this).find('input[type=hidden]').val()); 
}); 

Sample 2