當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


jQuery UI Sortable serialize()用法及代碼示例

jQuery UI  是一種基於 Web 的技術,由各種 GUI 小部件、視覺效果和主題組成。這些函數可以使用 jQuery JavaScript 庫來實現。 jQuery UI 是構建網頁 UI 接口的最佳工具。它還可用於構建高度交互的 Web 應用程序,或者可用於輕鬆添加小部件。

在本文中,我們將學習 jQuery UI Sortable serialize() 方法來生成哈希值,該哈希值可以附加到任何 URL,以便輕鬆地將新商品訂單提交回服務器。它將可排序的項目 ID 序列化為表單/ajax 可提交字符串。此方法使用 ID 作為默認屬性,例如,如果每個項目的 ID 為“geeks_value”,則將生成的哈希為“geeks[]=value & geeks[]=value”。

注意:如果 ID 屬性不包含下劃線,則序列化方法將返回空字符串。

用法:

調用serialize()方法:

$( "Selector" ).sortable( "serialize");

參數:該方法接受選項作為有助於自定義序列化的參數。它是一個對象類型。它包含3個值:

  • key: 它將part[]替換為指定值。它是字符串類型。
  • attribute:用於值 & 的屬性名稱是字符串類型。
  • expression:正則表達式用於將屬性值拆分為鍵值部分,且類型為RegExp。

返回類型:它返回 String 類型的哈希值。

CDN 鏈接:添加項目所需的 jQuery Mobile 腳本:

<link rel=”stylesheet” href=”https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css”>
<script src= “https://code.jquery.com/jquery-1.12.4.js”></script>
<script src= “https://code.jquery.com/ui/1.12.1/jquery-ui.js”></script>

例子:下麵的例子說明了 jQuery UI Sortable 的實現serialize()方法。

HTML


<!DOCTYPE html> 
<html> 
  
<head> 
    <meta charset="utf-8"> 
    <link rel="stylesheet" href= 
"https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> 
    <script src= 
"https://code.jquery.com/jquery-1.12.4.js"> 
    </script> 
    <script src= 
"https://code.jquery.com/ui/1.12.1/jquery-ui.js"> 
    </script> 
    <title>jQuery UI Sortable serialize method</title> 
    <style> 
        #sortableList { 
            list-style-type: none; 
        } 
        .geeks { 
            margin: 10px; 
            background: lightgreen; 
            padding: 10px; 
            border: 1px solid green; 
            font-size: 25px; 
        } 
    </style> 
    <script> 
        $(function() { 
            $("#sortableList").sortable({ 
                update: function(event, ui) { 
                    var hash = $(this).sortable('serialize'); 
                    $("#sortedList").text(hash); 
                } 
            }); 
        }); 
    </script> 
</head> 
  
<body> 
    <center> 
        <h1 style="color: green;"> 
            GeeksforGeeks 
        </h1> 
          
        <h2>jQuery UI Sortable serialize method</h2> 
          
        <ul id="sortableList"> 
            <li id="Tutorials_One" class="geeks"> 
                1.Free Tutorials  
            </li> 
            <li id="Articles_Two" class="geeks"> 
                2.Millions of articles  
            </li> 
            <li id="Webinars_Three" class="geeks"> 
                3.Webinars by Industry Experts  
            </li> 
            <li id="Courses_Four" class="geeks"> 
                4.Live, Online and Classroom Courses  
            </li> 
        </ul> 
          
        <h2>Hash produces by serialize method is </h2> 
          
        <h2> 
            <span id='sortedList'></span> 
        </h2>  
    </center> 
</body> 
</html>

輸出:

jQuery UI Sortable serialize() Method

jQuery UI 可排序 serialize() 方法

參考:https://api.jqueryui.com/sortable/#method-serialize



相關用法


注:本文由純淨天空篩選整理自harshsethi2000大神的英文原創作品 jQuery UI Sortable serialize() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。