select2 bootstrap 简单使用

今天无意发现bootstrap 中的下拉列表样式很好看,而且支持模糊查找.于是想研究看看发现使用select2插件

官网http://ivaynberg.github.io/select2/index.html


Constructor

Parameter Type Description
width string Controls thewidthstyle attribute of the Select2 container div. The following values are supported: off
No width attribute will be set. Keep in mind that the container div copies classes from the source element so setting the width attribute may not always be necessary.
element
Uses javascript to calculate the width of the source element.
copy
Copies the value of the width style attribute set on the source element.
resolve
First attempts to copythan falls back on element.
other values
if the width attribute contains a function it will be evaluated, otherwise the value is used verbatim.
minimumInputLength int Number of characters necessary to start a search.
maximumInputLength int Maximum number of characters that can be entered for an input.
minimumResultsForSearch int

The minimum number of results that must be initially (after opening the dropdown for the first time) populated in order to keep the search field. This is useful for cases where local data is used with just a few results, in which case the search box is not very useful and wastes screen space.

The option can be set to anegative valueto permanently hide the search field.

Only applies to single-value select boxes

maximumSelectionSize int/function

The maximum number of items that can be selected in a multi-select control. If this number is less than 1 selection is not limited.

Once the number of selected items reaches the maximum specified the contents of the dropdown will be populated by theformatSelectionTooBigfunction.

placeholder string

Initial value that is selected if no other selection is made.

The placeholder can also be specified as adata-placeholderattribute on theselectorinputelement that Select2 is attached to.

Note that because browsers assume the firstoptionelement is selected in non-multi-value select boxes an empty firstoptionelement must be provided (<option></option>) for the placeholder to work.

placeholderOption function/string

When attached to aselectresolves theoptionthat should be used as the placeholder. Can either be a function which given theselectelement should return theoptionelement or a stringfirstto indicate that the first option should be used.

This option is useful when Select2's default of using the first option only if it has no value and no text is not suitable.

separator string

Separator character or string used to delimit ids invalueattribute of the multi-valued selects. The default delimiter is the,character.

allowClear boolean

Whether or not a clear button is displayed when the select box has a selection. The button, when clicked, resets the value of the select box back to the placeholder, thus this option is only available when the placeholder is specified.

This option only works when the placeholder is specified.

When attached to aselectanoptionwith an empty value must be provided. This is the option that will be selected when the button is pressed since a select box requires at least one selectionoption.

Also, note that this option only works with non-multi-value based selects because multi-value selects always provide such a button for every selected option.

multiple boolean

Whether or not Select2 allows selection of multiple values.

When Select2 is attached to aselectelement this value will be ignored andselect'smultipleattribute will be used instead.

closeOnSelect boolean

If set to false the dropdown is not closed after a selection is made, allowing for rapid selection of multiple items. By default this option is set totrue.

Only applies when configured in multi-select mode.

openOnEnter boolean

If set to true the dropdown is opened when the user presses the enter key and Select2 is closed. By default this option is enabled.

id function Function used to get the id from the choice object or a string representing the key under which the id is stored.
id(object)
Parameter Type Description
object object A choice object.
<returns> string the id of the object.
The default implementation expects the object to have aidproperty that is returned.
matcher function Used to determine whether or not the search term matches an option when a built-in query function is used. The built in query function is used when Select2 is attached to aselect, or thelocalortagshelpers are used.
matcher(term, text, option)
Parameter Type Description
term string search term.
text string text of the option being matched.
option jquery object theoptionelement we are trying to match. Only given when attached toselect. Can be used to match against custom attributes on theoptiontag in addition to matching on theoption's text.
<returns> boolean trueif search term matches the text, orfalseotherwise.
The default implementation is case insensitive and matches anywhere in the term:function(term, text) { return text.toUpperCase().indexOf(term.toUpperCase())>=0; }
sortResults function Used to sort the results list for searching right before display. Useful for sorting matches by relevance to the user's search term.
sortResults(results, container, query)
.
object object One of the result objects returned from thequeryfunction
container jQuery object jQuery wrapper of the node that should contain the representation of the result.
query object The query object used to request this set of results.
<returns> object A results object.
Defaults to no sorting:function(results, container, query) { return results; }
formatSelection function Function used to render the current selection.
formatSelection(object, container)
Parameter Type Description
object object The selected result object returned from thequeryfunction.
container jQuery object jQuery wrapper of the node to which the selection should be appended.
escapeMarkup function Function that can be used to escape html markup. This is the function defined in theescapeMarkupoption, or the default.
<returns> string (optional) Html string, a DOM element, or a jQuery object that renders the selection.

The default implementation expects the object to have atextproperty that is returned.

The implementation may choose to append elements directly to the providedcontainerobject, or return a single value and have it automatically appended.



When attached to aselectthe original<option>(or <optgroup>) element is accessible inside the specified function through the propertyitem.element:

format(item) {
    var originalOption = item.element;
    return item.text
}

formatResult function Function used to render a result that the user can select.
formatResult(object, container, query)
Parameter Type Description
object object One of the result objects returned from thequeryfunction.
container jQuery object jQuery wrapper of the node that should contain the representation of the result.
query object The query object used to request this set of results.
escapeMarkup function Function used to escape markup in results. If you do not expect to render custom markup you should pass your text through this function to escape any markup that may have been accidentally returned. This function is configurable in options of select2.
<returns> string (optional) Html string, a DOM element, or a jQuery object that represents the result.

The default implementation expects the object to have atextproperty that is returned.

The implementation may choose to append elements directly to the providedcontainerobject, or return a single value and have it automatically appended.



When attached to aselectthe original<option>(or <optgroup>) element is accessible inside the specified function through the propertyitem.element:

format(item) {
    var originalOption = item.element;
    return item.text
}

formatResultCssClass function Function used to add css classes to result elements.
formatResultCssClass(object)
Parameter Type Description
object object One of the result objects returned from thequeryfunction.
<returns> string (optional) String containing css class names separated by a space.

By default when attached to aselectcss classes fromoptions will be automatically copied.

formatNoMatches string/function String containing "No matches" message, or
Function used to render the message
formatNoMatches(term)
Parameter Type Description
term string Search string entered by user.
<returns> string Message html.
formatSearching string/function String containing "Searching..." message, or
Function used to render the message that is displayed while search is in progress.
formatSearching()
Parameter Type Description
<returns> string Message html ornull/undefinedto disable the message.
formatInputTooShort string/function String containing "Search input too short" message, or
Function used to render the message.
formatInputTooShort(term, minLength)
Parameter Type Description
term string Search string entered by user.
minLength int Minimum required term length.
<returns> string Message html.
formatInputTooLong string/function String containing "Search input too long" message, or
Function used to render the message.
formatInputTooLong(term, maxLength)
Parameter Type Description
term string Search string entered by user.
maxLength int Maximum required term length.
<returns> string Message html.
formatSelectionTooBig string/function String containing "You cannot select any more choices" message, or
Function used to render the message.
formatSelectionTooBig(maxSize)
Parameter Type Description
maxSize string The maximum specified size of the selection.
<returns> string Message html.
formatLoadMore string/function String containing "Loading more results…" message, or
Function used to render the message.
formatLoadMore(pageNumber)
Parameter Type Description
pageNumber string The current page.
<returns> string Message html.
createSearchChoice function Creates a new selectable choice from user's search term. Allows creation of choices not available via the query function. Useful when the user can create choices on the fly, eg for the 'tagging' usecase.
createSearchChoice(term)
Parameter Type Description
term string Search string entered by user.
<returns> object (optional) Object representing the new choice. Must at least contain anidattribute.
If the function returnsundefinedornullno choice will be created. If a new choice is created it is displayed first in the selection list so that user may select it by simply pressingenter.

When used in combination withinput[type=hidden]tag care must be taken to sanitize theidattribute of the choice object, especially stripping,as it is used as a value separator.

createSearchChoicePosition string|function Define the position where to insert element created bycreateSearchChoice. The following values are supported: top
Insert in the top of the list
bottom
Insert at the end of the list
<function>

A custom function. For example if you want to insert the new item in the second position:

$("#tags").select2({
    ...
    createSearchChoice: function(term) { ... },
    createSearchChoicePosition: function(list, item) {
        list.splice(1, 0, item);
    }
});
initSelection function Called when Select2 is created to allow the user to initialize the selection based on the value of the element select2 is attached to.

Essentially this is anid->objectmapping function.

initSelection(element, callback)
Parameter Type Description
element jQuery array element Select2 is attached to.
callback function callback function that should be called with the data which is either an object in case of a single select or an array of objects in case of multi-select.

This function will only be called when there is initial input to be processed.

Here is an example implementation used for tags. Tags are the simplest form of data where the id is also the text:
$("#tags").select2({
    initSelection : function (element, callback) {
        var data = [];
        $(element.val().split(",")).each(function () {
            data.push({id: this, text: this});
        });
        callback(data);
    }
});

// Or for single select elements:
$("#select").select2({
    initSelection : function (element, callback) {
        var data = {id: element.val(), text: element.val()};
        callback(data);
    }
});
tokenizer function A tokenizer function can process the input typed into the search field after every keystroke and extract and select choices. This is useful, for example, in tagging scenarios where the user can create tags quickly by separating them with a comma or a space instead of pressing enter.

Tokenizer only applies to multi-selects.

tokenizer(input, selection, selectCallback, opts)
Parameter Type Description
input string The text entered into the search field so far.
selection array Array of objects representing the current selection. Useful if tokenizer needs to filter out duplicates.
selectCallback function Callback that can be used to add objects to the selection.
opts object Options with which Select2 was initialized. Useful if tokenizer needs to access some properties in the options.
<returns> string (optional) Returns the string to which the input of the search field should be set to. Usually this is the remainder, of any, of the string after the tokens have been stripped. Ifundefinedornullis returned the input of the search field is unchanged.
The default tokenizer will only be used if thetokenSeparatorsandcreateSearchChoiceoptions are specified. The default tokenizer will split the string using any separator intokenSeparatorsand will create and select choice objects usingcreateSearchChoiceoption. It will also ignore duplicates, silently swallowing those tokens.
tokenSeparators array An array of strings that define token separators for the defaulttokenizerfunction. By default, this option is set to an empty array which means tokenization using the default tokenizer is disabled. Usually it is sensible to set this option to a value similar to[',', ' '].
query function Function used to query results for the search term.
query(options)
Parameter Type Description
options.element jquery object The element Select2 is attached to.
options.term string Search string entered by user.
options.page int 1-based page number tracked by Select2 for use with infinite scrolling of results.
options.context object An object that persists across the lifecycle of queries for the same search term (the query to retrieve the initial results, and subsequent queries to retrieve more result pages for the same search term). When this function is first called for a new search term this object will be null. The user may choose to set any object in theresults.contextfield - this object will then be used as the context parameter for all calls to thequerymethod that will load more search results for the initial search term. The object will be reset back to null when a new search term is queried. This feature is useful when a page number is not easily mapped against the server side paging mechanism. For example, some server side paging mechanism may return a "continuation token" that needs to be passed back to them in order to retrieve the next page of search results.
options.callback function Callback function that should be called with theresultobject. The result object: Parameter Type Description
result.results [object] Array of result objects. The default renderers expect objects withidandtextkeys. Theidproperty is required, even if custom renderers are used. The object may also contain achildrenkey if hierarchical data is displayed. The object may also contain adisabledboolean property indicating whether this result can be selected.
result.more boolean trueif more results are available for the current search term.
results.context object A user-defined object that should be made available as thecontextparameter to thequeryfunction on subsequent queries to load more result pages for the same search term. See the description ofoptions.contextparameter.

In order for this function to work Select2 should be attached to ainput type='hidden'tag instead of aselect.

Example Data
{
     more: false,
     results: [
        { id: "CA", text: "California" },
        { id: "AL", text: "Alabama" }
     ]
}
                        

Example Hierarchical Data
{
    more: false,
    results: [
        { text: "Western", children: [
            { id: "CA", text: "California" },
            { id: "AZ", text: "Arizona" }
        ] },
        { text: "Eastern", children: [
            { id: "FL", text: "Florida" }
        ] }
    ]
}
                        

ajax object Options for the built in ajax query function. This object acts as a shortcut for having to manually write a function that performs ajax requests. The built-in function supports more advanced features such as throttling and dropping out-of-order responses. Parameter Type Description
transport function Function that will be used to perform the ajax request. Must be parameter-compatible with$.ajax. Defaults to$.ajaxif not specified. Allows the use of various ajax wrapper libraries such as:AjaxManager.
url string/function String containing the ajax url or a function that returns such a string.
dataType string Data type for the request.xml,json,jsonp, other formats supported by jquery.
quietMillis int Number of milliseconds to wait for the user to stop typing before issuing the ajax request.
cache boolean If set tofalse, it will force requested pages not to be cached by the browser. Default isfalse.
jsonpCallback string/function The callback function name for a JSONP request. This value will be used instead of the random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name as it'll make it easier to manage the requests and provide callbacks and error handling. You may want to specify the callback when you want to enable better browser caching of GET requests.
data function Function to generate query parameters for the ajax request.
data(term, page)
Parameter Type Description
term string Search term.
page int 1-based page number tracked by Select2 for use with infinite scrolling of results.
context object Seeoptions.contextparameter to thequeryfunction above.
<returns> object Object containing url parameters.
results function Function used to build the query results object from the ajax response
results(data, page)
Parameter Type Description
data object Retrieved data.
page int Page number that was passed into thedatafunction above.
context object Seeoptions.contextparameter to thequeryfunction above.
<returns> object Results object. See "options.callback" in the "query" function for format.
params object/function An object or a function that returns an object that contains extra parameters that will be passed to the transport. For example it can be used to set the content type:{contentType: "application/json;charset=utf-8"}

In order for this function to work Select2 should be attached to ainput type='hidden'tag instead of aselect.

For documentation of the data format see thequeryfunction.

data array/object Options for the built in query function that works with arrays.

If this element contains an array, each element in the array must containidandtextkeys.

Alternatively, this element can be specified as an object in whichresultskey must contain the data as an array and atextkey can either be the name of the key in data items that contains text or a function that retrieves the text given a data element from the array.

tags array/function Puts Select2 into 'tagging'mode where the user can add new choices and pre-existing tags are provided via this options attribute which is either anarrayor afunctionthat returns an array ofobjectsorstrings. Ifstringsare used instead ofobjectsthey will be converted into an object that has anidandtextattribute equal to the value of thestring.
containerCss function/object Inline css that will be added to select2's container. Either an object containing css property/value key pairs or a function that returns such an object.
containerCssClass function/string Css class that will be added to select2's container tag.
dropdownCss function/object Inline css that will be added to select2's dropdown container. Either an object containing css property/value key pairs or a function that returns such an object.
dropdownCssClass function/string Css class that will be added to select2's dropdown container.
dropdownAutoWidth boolean When set totrueattempts to automatically size the width of the dropdown based on content inside.
adaptContainerCssClass function Function that filters/renames css classes as they are copied from the source tag to the select2 container tag.
adaptContainerCssClass(clazz)
Parameter Type Description
clazz string Css class being copied.
<returns> string Css class to be applied ornull/undefined/''to not apply it.
The default implementation applies all classes without modification.
adaptDropdownCssClass function Function that filters/renames css classes as they are copied from the source tag to the select2 dropdown tag.
adaptDropdownCssClass(clazz)
Parameter Type Description
clazz string Css class being copied.
<returns> string Css class to be applied ornull/undefined/''to not apply it.
The default implementation always returnsnullthereby filtering out all classes.
escapeMarkup function String escapeMarkup(String markup)

Function used to post-process markup returned from formatter functions. By default this function escapes html entities to prevent javascript injection.

selectOnBlur boolean

Set totrueif you want Select2 to select the currently highlighted option when it is blurred.

loadMorePadding integer Defines how many pixels need to be below the fold before the next page is loaded. The default value is0which means the result list needs to be scrolled all the way to the bottom for the next page of results to be loaded. This option can be used to trigger the load sooner, possibly resulting in a smoother user experience.
nextSearchTerm function

Function used to determine what the next search term should be.

Parameter Type Description
data object Retrieved data.
this.search.val() string Search term that yielded the current result set.

Here is an example implementation used to display the current search term when the dropdown is opened:

            function displayCurrentValue(selectedObject, currentSearchTerm) {
                return currentSearchTerm;
            }

            $("#e1").select2({
                nextSearchTerm: displayCurrentValue
            });
            

Function can be used when the dropdown is configured in single and multi-select mode. It is triggered after selecting an item. In single mode it is also triggered after initSelection (when provided).

val

Gets or sets the selection. If thevalueparameter is not specified, theidattribute of the currently selected element is returned. If thevalueparameter is specified it will become the current selection.

Parameter Type Description
value (optional) object
Single-Valued Multi-Valued Attached toselect Attached toinput[type=hidden]
Value of thevalueattribute of theoptionthat should be selected. Array of thevalueattributes of theoptions that should be selected.nullfor empty.
Id of the object that should be selected.""to clear. Can only be used ifinitSelection()was specified. An array of objects ids that should be selected.""to clear. Can only be used ifinitSelection()was specified.
triggerChange (optional) boolean Whether or not achangeevent should be triggered.falseby default.

valmethod invoked on a single-select with an unset value will return"", while avalmethod invoked on an empty multi-select will return[].

Example:
alert("Selected value is: "+$("#e8").select2("val")); $("#e8").select2("val", "CA");


Html代码   收藏代码
  1. <div class="form-group">  
  2.                 <label class="control-label visible-ie8 visible-ie9">Country</label>  
  3.                 <select name="country" id="select2_sample4" class="select2 form-control">  
  4.                     <option value=""></option>  
  5.                     <option value="AF">Afghanistan</option>  
  6.                     <option value="AL">Albania</option>  
  7.                     <option value="DZ">Algeria</option>  
  8.                     <option value="AS">American Samoa</option>  
  9.                     <option value="AD">Andorra</option>  
  10.                     <option value="AO">Angola</option>  
  11.                     <option value="AI">Anguilla</option>  
  12.                     <option value="AQ">Antarctica</option>  
  13.                     <option value="AR">Argentina</option>  
  14.                     <option value="AM">Armenia</option>  
  15.                     <option value="AW">Aruba</option>  
  16.                     <option value="AU">Australia</option>  
  17.                     <option value="AT">Austria</option>  
  18.                     <option value="AZ">Azerbaijan</option>  
  19.                     <option value="BS">Bahamas</option>  
  20.                     <option value="BH">Bahrain</option>  
  21.                     <option value="BD">Bangladesh</option>  
  22.                     <option value="BB">Barbados</option>  
  23.                     <option value="BY">Belarus</option>  
  24.                     <option value="BE">Belgium</option>  
  25.                     <option value="BZ">Belize</option>  
  26.                     <option value="BJ">Benin</option>  
  27.                     <option value="BM">Bermuda</option>  
  28.                     <option value="BT">Bhutan</option>  
  29.                     <option value="BO">Bolivia</option>  
  30.                     <option value="BA">Bosnia and Herzegowina</option>  
  31.                     <option value="BW">Botswana</option>  
  32.                     <option value="BV">Bouvet Island</option>  
  33.                     <option value="BR">Brazil</option>  
  34.                     <option value="IO">British Indian Ocean Territory</option>  
  35.                     <option value="BN">Brunei Darussalam</option>  
  36.                     <option value="BG">Bulgaria</option>  
  37.                     <option value="BF">Burkina Faso</option>  
  38.                     <option value="BI">Burundi</option>  
  39.                     <option value="KH">Cambodia</option>  
  40.                     <option value="CM">Cameroon</option>  
  41.                     <option value="CA">Canada</option>  
  42.                     <option value="CV">Cape Verde</option>  
  43.                     <option value="KY">Cayman Islands</option>  
  44.                     <option value="CF">Central African Republic</option>  
  45.                     <option value="TD">Chad</option>  
  46.                     <option value="CL">Chile</option>  
  47.                     <option value="CN">China</option>  
  48.                     <option value="CX">Christmas Island</option>  
  49.                     <option value="CC">Cocos (Keeling) Islands</option>  
  50.                     <option value="CO">Colombia</option>  
  51.                     <option value="KM">Comoros</option>  
  52.                     <option value="CG">Congo</option>  
  53.                     <option value="CD">Congo, the Democratic Republic of the</option>  
  54.                     <option value="CK">Cook Islands</option>  
  55.                     <option value="CR">Costa Rica</option>  
  56.                     <option value="CI">Cote d'Ivoire</option>  
  57.                     <option value="HR">Croatia (Hrvatska)</option>  
  58.                     <option value="CU">Cuba</option>  
  59.                     <option value="CY">Cyprus</option>  
  60.                     <option value="CZ">Czech Republic</option>  
  61.                     <option value="DK">Denmark</option>  
  62.                     <option value="DJ">Djibouti</option>  
  63.                     <option value="DM">Dominica</option>  
  64.                     <option value="DO">Dominican Republic</option>  
  65.                     <option value="EC">Ecuador</option>  
  66.                     <option value="EG">Egypt</option>  
  67.                     <option value="SV">El Salvador</option>  
  68.                     <option value="GQ">Equatorial Guinea</option>  
  69.                     <option value="ER">Eritrea</option>  
  70.                     <option value="EE">Estonia</option>  
  71.                     <option value="ET">Ethiopia</option>  
  72.                     <option value="FK">Falkland Islands (Malvinas)</option>  
  73.                     <option value="FO">Faroe Islands</option>  
  74.                     <option value="FJ">Fiji</option>  
  75.                     <option value="FI">Finland</option>  
  76.                     <option value="FR">France</option>  
  77.                     <option value="GF">French Guiana</option>  
  78.                     <option value="PF">French Polynesia</option>  
  79.                     <option value="TF">French Southern Territories</option>  
  80.                     <option value="GA">Gabon</option>  
  81.                     <option value="GM">Gambia</option>  
  82.                     <option value="GE">Georgia</option>  
  83.                     <option value="DE">Germany</option>  
  84.                     <option value="GH">Ghana</option>  
  85.                     <option value="GI">Gibraltar</option>  
  86.                     <option value="GR">Greece</option>  
  87.                     <option value="GL">Greenland</option>  
  88.                     <option value="GD">Grenada</option>  
  89.                     <option value="GP">Guadeloupe</option>  
  90.                     <option value="GU">Guam</option>  
  91.                     <option value="GT">Guatemala</option>  
  92.                     <option value="GN">Guinea</option>  
  93.                     <option value="GW">Guinea-Bissau</option>  
  94.                     <option value="GY">Guyana</option>  
  95.                     <option value="HT">Haiti</option>  
  96.                     <option value="HM">Heard and Mc Donald Islands</option>  
  97.                     <option value="VA">Holy See (Vatican City State)</option>  
  98.                     <option value="HN">Honduras</option>  
  99.                     <option value="HK">Hong Kong</option>  
  100.                     <option value="HU">Hungary</option>  
  101.                     <option value="IS">Iceland</option>  
  102.                     <option value="IN">India</option>  
  103.                     <option value="ID">Indonesia</option>  
  104.                     <option value="IR">Iran (Islamic Republic of)</option>  
  105.                     <option value="IQ">Iraq</option>  
  106.                     <option value="IE">Ireland</option>  
  107.                     <option value="IL">Israel</option>  
  108.                     <option value="IT">Italy</option>  
  109.                     <option value="JM">Jamaica</option>  
  110.                     <option value="JP">Japan</option>  
  111.                     <option value="JO">Jordan</option>  
  112.                     <option value="KZ">Kazakhstan</option>  
  113.                     <option value="KE">Kenya</option>  
  114.                     <option value="KI">Kiribati</option>  
  115.                     <option value="KP">Korea, Democratic People's Republic of</option>  
  116.                     <option value="KR">Korea, Republic of</option>  
  117.                     <option value="KW">Kuwait</option>  
  118.                     <option value="KG">Kyrgyzstan</option>  
  119.                     <option value="LA">Lao People's Democratic Republic</option>  
  120.                     <option value="LV">Latvia</option>  
  121.                     <option value="LB">Lebanon</option>  
  122.                     <option value="LS">Lesotho</option>  
  123.                     <option value="LR">Liberia</option>  
  124.                     <option value="LY">Libyan Arab Jamahiriya</option>  
  125.                     <option value="LI">Liechtenstein</option>  
  126.                     <option value="LT">Lithuania</option>  
  127.                     <option value="LU">Luxembourg</option>  
  128.                     <option value="MO">Macau</option>  
  129.                     <option value="MK">Macedonia, The Former Yugoslav Republic of</option>  
  130.                     <option value="MG">Madagascar</option>  
  131.                     <option value="MW">Malawi</option>  
  132.                     <option value="MY">Malaysia</option>  
  133.                     <option value="MV">Maldives</option>  
  134.                     <option value="ML">Mali</option>  
  135.                     <option value="MT">Malta</option>  
  136.                     <option value="MH">Marshall Islands</option>  
  137.                     <option value="MQ">Martinique</option>  
  138.                     <option value="MR">Mauritania</option>  
  139.                     <option value="MU">Mauritius</option>  
  140.                     <option value="YT">Mayotte</option>  
  141.                     <option value="MX">Mexico</option>  
  142.                     <option value="FM">Micronesia, Federated States of</option>  
  143.                     <option value="MD">Moldova, Republic of</option>  
  144.                     <option value="MC">Monaco</option>  
  145.                     <option value="MN">Mongolia</option>  
  146.                     <option value="MS">Montserrat</option>  
  147.                     <option value="MA">Morocco</option>  
  148.                     <option value="MZ">Mozambique</option>  
  149.                     <option value="MM">Myanmar</option>  
  150.                     <option value="NA">Namibia</option>  
  151.                     <option value="NR">Nauru</option>  
  152.                     <option value="NP">Nepal</option>  
  153.                     <option value="NL">Netherlands</option>  
  154.                     <option value="AN">Netherlands Antilles</option>  
  155.                     <option value="NC">New Caledonia</option>  
  156.                     <option value="NZ">New Zealand</option>  
  157.                     <option value="NI">Nicaragua</option>  
  158.                     <option value="NE">Niger</option>  
  159.                     <option value="NG">Nigeria</option>  
  160.                     <option value="NU">Niue</option>  
  161.                     <option value="NF">Norfolk Island</option>  
  162.                     <option value="MP">Northern Mariana Islands</option>  
  163.                     <option value="NO">Norway</option>  
  164.                     <option value="OM">Oman</option>  
  165.                     <option value="PK">Pakistan</option>  
  166.                     <option value="PW">Palau</option>  
  167.                     <option value="PA">Panama</option>  
  168.                     <option value="PG">Papua New Guinea</option>  
  169.                     <option value="PY">Paraguay</option>  
  170.                     <option value="PE">Peru</option>  
  171.                     <option value="PH">Philippines</option>  
  172.                     <option value="PN">Pitcairn</option>  
  173.                     <option value="PL">Poland</option>  
  174.                     <option value="PT">Portugal</option>  
  175.                     <option value="PR">Puerto Rico</option>  
  176.                     <option value="QA">Qatar</option>  
  177.                     <option value="RE">Reunion</option>  
  178.                     <option value="RO">Romania</option>  
  179.                     <option value="RU">Russian Federation</option>  
  180.                     <option value="RW">Rwanda</option>  
  181.                     <option value="KN">Saint Kitts and Nevis</option>  
  182.                     <option value="LC">Saint LUCIA</option>  
  183.                     <option value="VC">Saint Vincent and the Grenadines</option>  
  184.                     <option value="WS">Samoa</option>  
  185.                     <option value="SM">San Marino</option>  
  186.                     <option value="ST">Sao Tome and Principe</option>  
  187.                     <option value="SA">Saudi Arabia</option>  
  188.                     <option value="SN">Senegal</option>  
  189.                     <option value="SC">Seychelles</option>  
  190.                     <option value="SL">Sierra Leone</option>  
  191.                     <option value="SG">Singapore</option>  
  192.                     <option value="SK">Slovakia (Slovak Republic)</option>  
  193.                     <option value="SI">Slovenia</option>  
  194.                     <option value="SB">Solomon Islands</option>  
  195.                     <option value="SO">Somalia</option>  
  196.                     <option value="ZA">South Africa</option>  
  197.                     <option value="GS">South Georgia and the South Sandwich Islands</option>  
  198.                     <option value="ES">Spain</option>  
  199.                     <option value="LK">Sri Lanka</option>  
  200.                     <option value="SH">St. Helena</option>  
  201.                     <option value="PM">St. Pierre and Miquelon</option>  
  202.                     <option value="SD">Sudan</option>  
  203.                     <option value="SR">Suriname</option>  
  204.                     <option value="SJ">Svalbard and Jan Mayen Islands</option>  
  205.                     <option value="SZ">Swaziland</option>  
  206.                     <option value="SE">Sweden</option>  
  207.                     <option value="CH">Switzerland</option>  
  208.                     <option value="SY">Syrian Arab Republic</option>  
  209.                     <option value="TW">Taiwan, Province of China</option>  
  210.                     <option value="TJ">Tajikistan</option>  
  211.                     <option value="TZ">Tanzania, United Republic of</option>  
  212.                     <option value="TH">Thailand</option>  
  213.                     <option value="TG">Togo</option>  
  214.                     <option value="TK">Tokelau</option>  
  215.                     <option value="TO">Tonga</option>  
  216.                     <option value="TT">Trinidad and Tobago</option>  
  217.                     <option value="TN">Tunisia</option>  
  218.                     <option value="TR">Turkey</option>  
  219.                     <option value="TM">Turkmenistan</option>  
  220.                     <option value="TC">Turks and Caicos Islands</option>  
  221.                     <option value="TV">Tuvalu</option>  
  222.                     <option value="UG">Uganda</option>  
  223.                     <option value="UA">Ukraine</option>  
  224.                     <option value="AE">United Arab Emirates</option>  
  225.                     <option value="GB">United Kingdom</option>  
  226.                     <option value="US">United States</option>  
  227.                     <option value="UM">United States Minor Outlying Islands</option>  
  228.                     <option value="UY">Uruguay</option>  
  229.                     <option value="UZ">Uzbekistan</option>  
  230.                     <option value="VU">Vanuatu</option>  
  231.                     <option value="VE">Venezuela</option>  
  232.                     <option value="VN">Viet Nam</option>  
  233.                     <option value="VG">Virgin Islands (British)</option>  
  234.                     <option value="VI">Virgin Islands (U.S.)</option>  
  235.                     <option value="WF">Wallis and Futuna Islands</option>  
  236.                     <option value="EH">Western Sahara</option>  
  237.                     <option value="YE">Yemen</option>  
  238.                     <option value="ZM">Zambia</option>  
  239.                     <option value="ZW">Zimbabwe</option>  
  240.                 </select>  
  241.             </div>  


Javascript代码   收藏代码
  1. var handleRegister = function () {  
  2.   
  3.     function format(state) {  
  4.            if (!state.id) return state.text; // optgroup  
  5.            return "<img class='flag' src='assets/img/flags/" + state.id.toLowerCase() + ".png'/>" + state.text;  
  6.        }  
  7.   
  8.   
  9.     $("#select2_sample4").select2({  
  10.         placeholder: '<i class="fa fa-map-marker"></i>Select a Country',  
  11.            allowClear: true,  
  12.            formatResult: format,  
  13.            formatSelection: format,  
  14.            escapeMarkup: function (m) {  
  15.                return m;  
  16.            }  
  17.        });  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值