【问题标题】:Cycle through <select> / <option> list and collect values with PHP and Javascript?循环遍历 <select> / <option> 列表并使用 PHP 和 Javascript 收集值?
【发布时间】:2011-10-09 18:15:10
【问题描述】:

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

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

有什么想法吗?

非常感谢您。

灰烬。

【问题讨论】:

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

标签: php javascript selection option


【解决方案1】:

我使用 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()

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2020-12-22
      • 1970-01-01
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多