【问题标题】:How to pass array of values from multiselect into jquery ajax call?如何将多选中的值数组传递给jquery ajax调用?
【发布时间】:2014-05-09 01:25:24
【问题描述】:

我正在使用 jQuery multiSelect 并希望将选择传递给我的更新脚本。

这是我的 ajax 调用

var newnotesecurity = $("#nt_newnotesecurity").serializeArray();

$.ajax({
  url: 'notes-savenewnote.php',
  method: 'POST',
  data: {
    cache: false,
    companyid: companyid,
    entitytype: entitytype,
    entityid: entityid,
    noteinitials: newnoteinitials,
    notetext: newnotetext,
    notetags: newnotetags,
    notesecurity: newnotesecurity
  },
  success: function() {
    // blah
  },
  error: function(){
    // blah
  }
});

nt_newnotesecurity 是多选对象。显然 serializeArray 不适合我。这只是我最近的一次尝试。

我最终传递了这个:

notesecurity[0][name]=nt_newnotesecurity&
notesecurity[0][value]=2&
notesecurity[1][name]=nt_newnotesecurity&
notesecurity[1][value]=14

但我想传递这个

nt_newnotesecurity=2&nt_newnotesecurity=14

或者我会满足于传递一个我以后可以爆炸的值,就像这样

notesecurity=2-14

【问题讨论】:

    标签: jquery ajax jquery-multiselect


    【解决方案1】:

    根据多选插件的documentation,只需调用val() 即可获得所有值

    var newnotesecurity = $("#nt_newnotesecurity").val();
    

    【讨论】:

    • 谢谢。这很完美。我可以发誓我很早就尝试过,但那时我的代码肯定有其他问题。现在我觉得很傻。
    • @Vincent - 没问题,乐于助人!
    • 虽然这是五年前的事了,但我很确定它最初对我不起作用的原因是因为我忽略了将 multiple='multiple' 添加到 select 标记中。我知道,因为它今天早上又发生在我身上。
    猜你喜欢
    • 2010-12-27
    • 1970-01-01
    • 2011-07-09
    • 2019-06-05
    • 1970-01-01
    • 2015-01-16
    • 2015-08-21
    • 2019-09-03
    • 2012-11-18
    相关资源
    最近更新 更多