Here is a snippet of my code: http://jsfiddle.net/natatkinson/xbWEb/
Javascript:
$(document).ready(function () { $("#tags").select2({ maximumSelectionSize: 3 }); });
HTML:
<form name="form1" id="form1" action="" method="GET"> <fieldset> <legend>Shoe Info</legend> <dl> <dt>Name:</dt> <dd> <input type="text" name="name" id="name" size="40" placeholder="Shoe name" required autofocus> </dd> </dl> <dt>Tags:</dt> <dd> <select multiple="" name="tags" id="tags" style="width:100%;"> <option value="0">Select Tags</option> <option value="2">racing flat</option> <option value="3">track spikes</option> <option value="1">trainer</option> </select> </dd> </dl> </fieldset> <p class="submit-buttons"> <input type="submit" class="button1" name="submit" id="submit" value="Add Shoes" /> </p>
All that submits is 1 value, not a comma separated list like the documentation says.
To set selected value of jQuery Select2 with JavaScript, we call val and then trigger . Then we set its value with val to the option with value 0. Finally, we call trigger with 'change' to update the drop down to show the value we set.
Add Select All Option to jQuery Select2:First create the markup for the multi-value select box and a check box for selecting all option. Now add this java script to the page. The above script will be executed by onclick() event of the check box.
In order to clear the selection of those values which are selected using a Select2 drop down,we can use the empty() function. The dropdown can be reset using Jquery. $("#select2_example"). empty();
You must use [] for multiple value contained form control's name. Correct using at bottom:
<select multiple="" name="tags[]" id="tags" style="width:100%;">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With