使用jQuery,如何模拟select的表单序列化,并在$.ajax调用中选择多个选项?

使用jQuery,如何模拟select的表单序列化,并在$.ajax调用中选择多个选项?,jquery,Jquery,下面是我的$.ajax调用,如何在数据部分中放置一个或多个选定值 $.ajax({ type: "post", url: "http://myServer" , dataType: "text", data: { 'service' : 'myService', 'program' : 'myProgram', 'start' : start, 'end' : end , },

下面是我的$.ajax调用,如何在数据部分中放置一个或多个选定值

$.ajax({
    type: "post",
    url: "http://myServer" ,
    dataType: "text",
    data: {
        'service' : 'myService',
        'program' : 'myProgram',
        'start' : start,
        'end' : end ,
        },
    success: function(request) {
      result.innerHTML = request ;
    }   // End success
  }); // End ajax method
编辑我应该包括我了解如何使用以下代码循环选择所选选项:

$('#userid option').each(function(i) {
 if (this.selected == true) {

但我如何将其放入我的数据:部分?

使用数组如何

data: {
    ...
    'select' : ['value1', 'value2', 'value3'],
    ...
},
编辑:啊,对不起,这是代码,有几个注意事项:

'select' : $('#myselectbox').serializeArray(),
但为了使序列化数组工作,所有表单元素都必须具有name属性。上面“select”的值将是一个包含所选元素的名称和值的对象数组

'select' : [
    { 'name' : 'box', 'value' : 1 },
    { 'name' : 'box', 'value' : 2 }
],
生成上述结果的选择框为:

<select multiple="true" name="box" id="myselectbox">
   <option value="1" name="option1" selected="selected">One</option>
   <option value="2" name="option2" selected="selected">Two</option>
   <option value="3" name="option3">Three</option>
</select>

多亏了@Owen的回答,我才有了这个代码

对于id为mySelect multiple=true的选择框

    var mySelections = [];
    $('#mySelect option').each(function(i) {
        if (this.selected == true) {
            mySelections.push(this.value);
        }
    });


    $.ajax({
      type: "post",
      url: "http://myServer" ,
      dataType: "text",
      data: {
        'service' : 'myService',
        'program' : 'myProgram',
        'selected' : mySelections
        },
      success: function(request) {
        result.innerHTML = request ;
      }
    }); // End ajax method

表示所选多个选项集合的正确方法是使用数组,方法是使用[]后缀命名SELECT标记。 问题是jQuery方法serialize没有正确处理它。 对于这样的选择,事实上:

<select name="a[]"> <option value="five">5</option> <option value="six">6</option> <option value="seven">7</option> </select> serialize发送此数组:a[]=0&a[]=1&a[]=2 PHP通过以下方式接收:

[a] => Array ( [0] => 0 [1] => 1 [2] => 2 )
实际值丢失的地方。

您可能还需要选择器$:selected selectbox,其中selectbox是您的选择标记。当我阅读$ajax的文档时…具体地说是数据:,我得到了您的答案,即使用数组来“选择”…jQuery序列化作为数组输入的参数..因此您得到&select=parm1&select=parm2&select=parm3,thanksah很高兴您能使用它:只需一个注释,serialize将返回值&select=param等。。。而serializeArray将其放置在上面详述的结构中。干杯。你为什么认为这是一个PHP问题?其他语言通过POST知道如何处理数组