2025-07-07 13:45
Ajax
使用Jquery Chosen插件通过Ajax动态填充列表是一种非常方便的方法,它可以帮助我们实现在输入框中输入内容时自动从服务器获取相关选项,并以列表形式展示出来。这对于提升用户体验和减少用户输入错误非常有帮助。下面我们来看一下如何使用这个功能,并通过一个具体的案例来说明。
html<!DOCTYPE html><html><head> <Meta charset="UTF-8"> <title>使用Jquery Chosen插件通过Ajax动态填充列表</title> <link rel="stylesheet" class="url" target="_blank" rel="nofollow noreferrer" href="/to/?target=https://cdnJS.cloudflare.com/Ajax/libs/chosen/1.8.7/chosen.min.CSS"></head><body> <h2><strong>选择国家</strong></h2> <input type="text" id="country-input"> <h3><strong>可选国家</strong></h3> <select id="country-list" class="chosen-select" multiple></select> <script src="https://code.jquery.com/jquery-3.6.0.min.JS"></script> <script src="https://cdnJS.cloudflare.com/Ajax/libs/chosen/1.8.7/chosen.jquery.min.JS"></script> <script> $(document).ready(function(){ $("#country-input").on("input", function(){ var keyword = $(this).val(); $.Ajax({ url: "get_countries.php", data: { keyword: keyword }, success: function(data){ $("#country-list").empty(); $.each(data, function(index, item){ $("#country-list").append('<option value="' + item.value + '">' + item.text + '</option>'); }); $("#country-list").trigger("chosen:updated"); } }); }); $(".chosen-select").chosen(); }); </script></body></html>
JSon[ { "value": "China", "text": "中国" }, { "value": "United States", "text": "美国" }, { "value": "Japan", "text": "日本" }, { "value": "Germany", "text": "德国" }, { "value": "United Kingdom", "text": "英国" }]
关于我们|免责条款|版权声明|侵权举报|隐私政策|联系我们
Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号