[英]WooCommerce event not triggering when using Select2 instead of actual select
[英]Is there any way to identify what is triggering the select2 closing event?
有什么方法可以確定是什么觸發了 select2:closing 事件處理程序?
更具體地說,我希望能夠確定關閉事件是否被觸發,因為
根據我的發現,該事件僅包含有關它正在影響的目標 select2 的信息,而沒有有關觸發它的事件的任何信息。
與其使用單個事件,不如根據您想了解的內容使用不同的事件。 您始終可以將它們組合到一個事件處理程序中。
select2:select
Triggered whenever a result is selected
select2:close
下拉框關閉時觸發
select2:opening
或select2:open
select2:opening
在下拉菜單打開前觸發
select2:open
每當打開下拉列表時觸發(已打開)
然后您需要檢查是否已經選擇了一個值,但是預先選擇的值(加載時,當呈現 html 時)和用戶之前選擇的值之間沒有區別。 要知道這是否是“第二次”(而不是僅以選定的值打開),您還必須記錄下拉列表是“第一次”打開的。
select2:unselect
每當刪除選擇時觸發
(3) 的確切用例將決定是使用select2:opening
(打開之前)還是select2:open
(打開之后)
這是一個只允許用戶打開下拉列表兩次的示例 - 之后它被阻止打開:
$('.select2').select2(); $(document).on('select2:opening', function(event) { // get the open count from "this" select // so each select has its own count var openCount = $(this).data("opencount") || 0; // check if this is the "third" time (allow it to be opened twice) // could just as easily use ||1 above or >0 for "second time" if (openCount > 1) return false; // store on "this" select the open count $(this).data("opencount", ++openCount); });
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/css/select2.min.css" rel="stylesheet" /> <script src="//code.jquery.com/jquery-2.2.4.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.min.js"></script> <select class="select2" multiple> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="AZ">Arizona</option> </select>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.