2013-05-27 52 views
-3

我有一個列表框,一個文本框和一個html格式的按鈕。如何將選定的列表框複製到HTML格式的文本框中

當用戶從列表框中選擇一個值並按下按鈕時,列表框顯示一些值。所選值應該被複制到文本框的值。

可以任意1給我一個HTML和JavaScript代碼來做這個工具。

感謝

+3

與我們分享你所擁有的嘗試,然後我們可以看看 –

+0

發佈代碼的一部分 –

回答

1
<input type="button" onClick="copy()" value="Copy"/> 


function copy(){ 
document.getElementById("textBoxId").value = document.getElementById("selectBoxId").value 
} 
1

的Javascript:

function copyToTextBox() { 
    document.getElementById('textbox').value = document.getElementById('listbox').value; 
} 

在按鈕,做這樣的事情onclick="copyToTextBox()"

0
<html> 
<head> 
<title> Example</title> 
<script> 
function showSelection() 
{ 
    document.frm.tf.value = document.frm.list.value; 
} 
</script> 
<body> 
<form name='frm'> 
    <select name="list"> 
    <option name="o1"> Option 1</option> 
    </select> 
    <input type=text name="tf" /> 
    <input type=button name="btn" onclick="showSelection()" /> 
</form> 
</body> 
</html> 
1

Working jsFiddle Demo

考慮下面的標記:

<select id="myselect"> 
    <option>Apple</option> 
    <option>Banana</option> 
    <option>Kiwi</option> 
    <option>Orange</option> 
</select> 

<input type="text" id="mytext" /> 

<input type="button" id="mybutton" value="Copy" /> 

而且在你的JavaScript:

window.onload = function() { 
    // get necessary elements on the page 
    var mybutton = document.getElementById('mybutton'); 
    var myselect = document.getElementById('myselect'); 
    var mytext = document.getElementById('mytext'); 

    // whenever use click on the button 
    mybutton.onclick = function() { 
     // get current value of drop down 
     var text = myselect.options[myselect.selectedIndex].value; 

     // set it to the textbox 
     mytext.value = text; 
    }; 
}; 
相關問題