繁体   English   中英

绕圈穿过 <select> / <option>列出并使用PHP和Javascript收集值?

[英]Cycle through <select> / <option> list and collect values with PHP and Javascript?

请您帮帮我,我知道这是一个非常新手的问题,但是我以前从未真正使用过它,并且正努力在Google中找到相关的结果。

基本上我在<select> <option>里面有一组<option>并且想使用javascript删除所有已选择的值,将它们放入字符串中,然后将其发布到我的PHP脚本中...

有任何想法吗?

非常感谢你提前。

灰。

我使用jQuery编写了一个示例 ,因为它使解决方案非常简单。

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()

我仅使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM