Table of Contents
How to Select a Value in Select2?
Select2 < Version 4
Select2 Version 4 and above
Home Backend Development PHP Tutorial How to Pre-Select a Value in Select2: A Guide for Versions 4 and Below?

How to Pre-Select a Value in Select2: A Guide for Versions 4 and Below?

Oct 27, 2024 am 09:02 AM

How to Pre-Select a Value in Select2: A Guide for Versions 4 and Below?

How to Select a Value in Select2?

Select2 is a jQuery plugin that provides a customizable select box widget. In this article, we'll explore how to set a pre-selected value in Select2.

Select2 < Version 4

  1. HTML: First, define the Select2 input field as a hidden field with an ID:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<code class="html">&lt;input type=&quot;hidden&quot; name=&quot;programid&quot; id=&quot;programid&quot; class=&quot;width-500&quot;&gt;<ol start="2">

<li>

<strong>Initialize Select2:</strong> Create an instance of Select2 and configure it with the necessary options, including an AJAX call for retrieving data.</li>

<li>

<strong>Set Pre-Selected Value:</strong> Use the select2('data', { id: "selectedID", text: "selectedText" }) method to set the selected value.</li>

</ol>

<h3 id="Select-Version-and-above">Select2 Version 4 and above</h3>

</h3>

<p>For Select2 v4 and above, the approach differs slightly:</p>

<ol><li>

<strong>HTML:</strong> Define the Select2 input field as a standard select element with options and a selected attribute:</li></ol>

<pre class="brush:php;toolbar:false"><code class="html">&lt;select id=&quot;mySelect2&quot; name=&quot;mySelect2[]&quot;&gt;

  &lt;option value=&quot;TheID&quot; selected=&quot;selected&quot;&gt;The text&lt;/option&gt;                                                                  

&lt;/select&gt;</code>

Copy after login
  1. Append Options: To set a value after initialization, you can append pre-selected options to the Select2 field:

1

2

<code class="javascript">var $newOption = $("&lt;option selected='selected'&gt;&lt;/option&gt;").val("TheID").text("The text");

$("#mySelect2").append($newOption).trigger('change');</code>

Copy after login
  1. Use the val() Method: Alternatively, you can use the val() method followed by .trigger('change') to set a value:

1

<code class="javascript">$("#mySelect2").val(5).trigger('change');</code>

Copy after login

These methods provide a straightforward way to set a pre-selected value in Select2, regardless of the version you're using.

The above is the detailed content of How to Pre-Select a Value in Select2: A Guide for Versions 4 and Below?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles