问题描述:

请你能帮助我,我知道它是一个非常新的问题,但我从来没有真正使用过这个,并且努力在谷歌中找到相关的结果。通过<select>/<option>列出并通过PHP和Javascript收集值?

基本上我有一个<select>内一套<option>并希望使用JavaScript来删除已选择的所有值,把它们放入一个字符串,然后将其张贴到我的PHP脚本...

任何想法?

非常感谢您提前。

灰。

+0

这是什么意思“我里面有一组的”?请澄清。 –

只使用的JavaScript/DOM 我猜select有多个属性

var selectElem = document.getElementById('myselect'); // your select element 
var strSelection = ''; // string to store the selected stuff 
for(let count = 0, limit = selectElem.options.length; count < limit; count++) //check every option 
    if(selectElem.options[count].selected) // check if it's selected 
     strSelection += selectElem.options[count].value + ','; // Concat to string and add delimiter, string format is up to you, here i add the .value but could also be .text or both.. 
selectElem.selectedIndex = -1; //Resets the selection element so all options are unselected 

我写了一个使用jQuery的example,因为它使得解决方案非常简单。

var str = ""; 
// iterates over all selected options and generates the value-string. 
$('#id option:selected').each(function(k,el) { 
    str += $(el).val()+', '; 
}); 

// removes all selected options 
$('#id option:selected').remove(); 

将数据发送到服务器看看这jQuery函数:jQuery.get()jQuery.post()