1

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...

1

3 Answers 3

4

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);
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

This worked! I had to change it to data: { tags_selected: selectArr } and then it worked perfectly. Thank you :)
This worked. but if you want to get the selected values only, you can use $('#tags_selected').val();
0

You need to find the <option> descendants of the dropdown and get their values:

data: { 
  values: $('#tags_selected').find("option").map(function() {return this.value;})
}

This way on the server side you will get an array named "values" with all the values. I have used .find() instead of .children() to get the options in anticipation of the drop down containing <optgroup> elements.

4 Comments

This returned a error... Not enough arguments. Thank you for your answer, though!
@JobertEnamno: Slower than what?
@PatrickManser: I am entirely unsure what error you are referring to.
@Jon It just said "Not enough arguments" :/
0
You need to send both selected and all values array.

var data: { 
        allValues: $('#tags_selected').find("option").map(function() {return this.value;}),
        selectedValues :$('#tags_selected').val()
    }
var data={ }
$.ajax({
  url: base_url + 'companies/editTagsAsync',
  type: 'post',
  dataType: 'json',
  data: data,
  success: function (json) {
    console.log(json);
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.