jQuery获取多个select的所有值,不仅被选中 [英] jQuery get all values of multiple select, not only selected

查看:427
本文介绍了jQuery获取多个select的所有值,不仅被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过Ajax提交整个多重选择(即$('#tags_selected'))

I'm trying to submit a whole multiple select (which is $('#tags_selected')) via Ajax

$.ajax({
  url: base_url + 'companies/editTagsAsync',
  type: 'post',
  dataType: 'json',
  data: $('#tags_selected').val(),
  success: function (json) {
    console.log(json);
  }
});

但它仅发送所选的值。我想通过选择框中的所有值。这可能是一个简单的解决方案,但我只是不知道它...

But it only sends the values that are selected. I want to pass ALL the values in the selectbox. There's probably an easy solution to this, but I just don't know it...

推荐答案

您可以将所有选择值转换为数组并通过 data 发送它:

You can push all your select value into an array and send it through data:

var selectArr = []; 

$('#tags_selected option').each(function() {
    selectArr.push($(this).val());
});

$.ajax({
  url: base_url + 'companies/editTagsAsync',
  type: 'post',
  dataType: 'json',
  data: selectArr,
  success: function (json) {
    console.log(json);
  }
});

这篇关于jQuery获取多个select的所有值,不仅被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆