2

I need help with getting the selected data from my multiselect into my javascript datastring when the submit button is pressed.

So I have this select:

<select multiple class="form-control" name="DivisionSelect[]">
  <option value="|TD">Traffic Division</option>
  <option value="|CP">Metro - C Platoon</option>
  <option value="|DP">Metro - D Platoon</option>
</select>

I want the selected data to be lined next to each other like this: "|TD|DP" if Traffic and D Platoon are selected. How can I do this?

4
  • 1
    question is not clear Commented Apr 27, 2017 at 11:22
  • How are you creating datastring? Commented Apr 27, 2017 at 11:24
  • var dataString = 'Crime='+ Crime + '&Type='+ Type + '&DescriptionForm='+ DescriptionForm +'&PinID='+ PinID + '&CordX='+ CordX + '&CordY='+ CordY + '&UserID='+ UserID + '&DivSelect='+ UseDiv + '&Divs='+ selecteddivs; Commented Apr 27, 2017 at 11:25
  • @St3fanNL i added submit example in my answer. Commented Apr 27, 2017 at 11:32

4 Answers 4

2

You can use join() on select value and add it to string.

$('select').change(function() {
  var str = '&Divs="' + $(this).val().join('') + '"';
  console.log(str)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select multiple class="form-control" name="DivisionSelect[]">
  <option value="|TD">Traffic Division</option>
  <option value="|CP">Metro - C Platoon</option>
  <option value="|DP">Metro - D Platoon</option>
</select>

To get this value on form submit you can use submit event on form and get value of select using find

$('form').submit(function(e) {
  e.preventDefault();
  
  var value = $(this).find('select[name="DivisionSelect[]"]').val();
  var str = '&Divs="' + (value ? value.join('') : '') + '"';
  console.log(str)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="">
  <select multiple class="form-control" name="DivisionSelect[]">
  <option value="|TD">Traffic Division</option>
  <option value="|CP">Metro - C Platoon</option>
  <option value="|DP">Metro - D Platoon</option>
</select>
  <br>
  <input type="submit" value="Submit">
</form>

Sign up to request clarification or add additional context in comments.

1 Comment

How can I only make this work on the select with name DivisionSelect?
1

$("select").change(function() {

  var value = $("select option:selected").map(function() {


    return $(this).val()
  }).get().join("")

  console.log("&Divs="+value)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select multiple class="form-control" name="DivisionSelect[]">
  <option value="|TD">Traffic Division</option>
  <option value="|CP">Metro - C Platoon</option>
  <option value="|DP">Metro - D Platoon</option>
</select>

Use .map()

Description: Translate all items in an array or object to new array of items.

2 Comments

And how can I ge this under my "submit" form? Can I make it do this when someone hits the submit button?
yes just change the $("select").change(function() { with you `$("form").submit(function() {
0

I Think below code will work for you.

var querystring = "&DivisionSelect=";
$('[name="DivisionSelect"] :selected').each(function(i, selected){ 
  querystring += $(selected).val()+querystring; 
});
alert(querystring);

Comments

0

Hev you tried, to read out the selected option, and then appending them to a String? You can append via concat() :

var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2);

I hope this helps :)

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.