[英]Pass multiple select options text to textarea
我有一個表單,其中有幾個選擇框,其中一些是多選。 到目前為止,我能夠將每個選擇框中的文本值傳遞給textarea; 但是,我無法從多個選擇框中傳遞多個選項。 有人可以幫忙嗎? 這意味着,我想在多個選擇框中傳遞所有選定的文本,而不僅僅是選擇中的第一個項目。
我的代碼不斷返回'undefined'。 在搜索這些論壇時,似乎使用jQuery'選項:selected'修飾符應該返回所有選定項的文本。
function writeText(){
// get assign the values from each text input
var myCAPtext = document.getElementById('CAPoutput');
// assign multiple text values
var select_1 = $("#box1 option:selected").text();
var CAPtext = select_1.value+"\n\n";
myCAPtext.value = CAPtext;
}
HTML
<select id="box1" multiple>
<option></option>
<option>first text</option>
<option>second text</option>
<option>third text</option>
<option>fouth text</option>
</select>
<input value="Write" onclick="writeText()" type="button"> <br>
<textarea id="CAPoutput">
text
返回一個字符串,String對象沒有value
屬性,使用jQuery val
方法,它返回多個select元素的值數組,然后你可以使用數組對象的join
方法來連接數組的元素:
var select_1 = $("#box1").val();
var CAPtext = select_1.join("\n\n");
如果你想跟onclick
屬性說再見:
$('input[type=button]').click(function () {
var myCAPtext = document.getElementById('CAPoutput');
myCAPtext.value = $("#box1").val().join("\n\n");
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.