【问题标题】:Send list of :selected from multiple selectable select via getJSON发送列表:通过 getJSON 从多个可选择的选择中选择
【发布时间】:2011-01-20 11:40:21
【问题描述】:

我在多选元素中有一个日期列表,我需要将所选日期列表发送到服务器进行处理。
jQuery 正在失控,这让我的内存和 CPU 使用率不断增加,我不明白单独运行失控部分就可以了。

HTML
<select id="my-dates" name="my-dates[]" multiple="multiple" size="5">
  <option>2011-01-18</option>
  <option>2011-01-20</option>
  <option>2011-01-21</option>
  <option>2011-01-27</option>
</select>

jQuery
$.getJSON('dates_handling.php',{
 dates: $('select#my-dates').find(':selected'),
 other:stuff
},function(result){
  // result handling
});
data key 'dates' should contain an transmit-able array like ['2011-01-20',2011-01-27']

jQuery haywire part
$('select#my-dates').find(':selected')

【问题讨论】:

    标签: json jquery getjson


    【解决方案1】:

    我会手动构建日期数组,因为您要传递完整的 jQuery 对象:

    var selectedDates = [];
    
    $('select#my-dates > option:selected').each(function() {
        selectedDates.push($(this).html());
    });
    
    $.getJSON('dates_handling.php',{
     dates: selectedDates,
     other:stuff
    },function(result){
      // result handling
    });
    

    【讨论】:

      【解决方案2】:

      你也可以使用:

      $('#my-dates').val()
      

      它将选择的值作为数组返回。

      【讨论】:

        猜你喜欢
        • 2020-03-21
        • 1970-01-01
        • 2016-03-03
        • 1970-01-01
        • 2021-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多