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


jQWidgets jqxComboBox getSelectedItems()用法及代碼示例


jQWidgets 是一個 JavaScript 框架,用於為 PC 和移動設備製作基於 Web 的應用程序。它是一個非常強大和優化的框架,獨立於平台,並得到廣泛支持。 jqxComboBox 用於表示一個 jQuery 組合框小部件,它包含一個具有自動完成函數的輸入字段和一個顯示在下拉列表中的可選項目列表。

getSelectedItems() 方法用於在開啟 “multiselect” 時返回選中的項。返回值是一個帶有對象的數組。它不接受任何參數並返回一個元素數組。

返回值是一個具有以下字段的對象:

  • label
  • value
  • disabled
  • checked
  • hasThreeStates
  • html
  • index
  • group

用法:

var items = $('selector').jqxComboBox('getSelectedItems');


鏈接文件:從 https://www.jqwidgets.com/download/鏈接下載 jQWidgets。在 HTML 文件中,找到下載文件夾中的腳本文件:

<link rel=”stylesheet” href=”jqwidgets/styles/jqx.base.css” type=”text/css” />
<script type=”text/javascript” src=”scripts/jquery-1.11.1.min.js”></script>
<script type=”text/javascript” src=”jqwidgets/jqx-all.js”></script>
<script type=”text/javascript” src=”jqwidgets/jqxcore.js”></script>
<script type=”text/javascript” src=”jqwidgets/jqxcolorpicker.js”></script>
<script type=”text/javascript” src=”.jqwidgets/jqxbuttons.js”></script>
<script type=”text/javascript” src=”jqwidgets/jqxscrollbar.js”></script>
<script type=”text/javascript” src=”jqwidgets/jqxlistbox.js”></script>
<script type=”text/javascript” src=”jqwidgets/jqxcombobox.js”></script>

下麵的例子說明了 jQWidgets 中的 jqxComboBox getSelectedItems() 方法:

例:

HTML


<!DOCTYPE html>
<html lang="en">
  
<head>
    <link rel="stylesheet" href=
        "jqwidgets/styles/jqx.base.css" type="text/css" />
    <script type="text/javascript" 
        src="scripts/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" 
        src="jqwidgets/jqx-all.js"></script>
    <script type="text/javascript" 
        src="jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" 
        src="jqwidgets/jqxcolorpicker.js"></script>
    <script type="text/javascript" 
        src=".jqwidgets/jqxbuttons.js"></script>
    <script type="text/javascript" 
        src="jqwidgets/jqxscrollbar.js"></script>
    <script type="text/javascript" 
        src="jqwidgets/jqxlistbox.js"></script>
    <script type="text/javascript" 
        src="jqwidgets/jqxcombobox.js"></script>
</head>
  
<body>
    <center>
        <h1 style="color:green;">
            GeeksforGeeks
        </h1>
  
        <h3>
            jQWidgets jqxComboBox getSelectedItems() Method
        </h3>
  
        <div id='jqxCB'></div>
        <br>
        <input type="button" id='jqxBtn' 
            style="padding:5px 20px;"
            value="Get Selected Items List" />
    </center>
  
    <script type="text/javascript">
        $(document).ready(function () {
            var data = [
                "Computer Science",
                "C Programming",
                "C++ Programming",
                "Java Programming",
                "Python Programming",
                "HTML",
                "CSS",
                "JavaScript",
                "jQuery",
                "PHP",
                "Bootstrap"
            ];
  
            $("#jqxCB").jqxComboBox({
                source:data,
                width:'250px',
                animationType:'slide',
                multiSelect:true
            });
      
            $('#jqxBtn').on('click', function () {
                var items = $("#jqxCB").jqxComboBox('getSelectedItems');
  
                var str = "";
                for (var i = 0; i < items.length; i++) {
                    str += items[i].label;
                    if (i < items.length - 1) str += ", ";
                }
                alert(str);
            });
        });
    </script>
</body>
  
</html>

輸出:

參考: https://www.jqwidgets.com/jquery-widgets-documentation/documentation/jqxcombobox/jquery-combobox-api.htm




相關用法


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