我無法獲得 select2 下拉串列的先前值
下面是下拉選單的設計部分

為了解決這個問題,我重試了多個代碼,但沒有一個對我有用。
以下是我嘗試過的代碼
const $country = $content.find('.input.LOSS_STATS_COUNTRY');
代碼 : 1
$country.on('change', function () {
debugger;
var newStatus = $(this).val();
var oldStatus = $(this).data('pre');
console.log(newStatus "-" oldStatus);
}).data('pre', $country.val());
代碼 : 2
$country.on('focusin', function(){
console.log("Saving value " $(this).val());
$(this).data('val', $(this).val());
});
$country.on('change', e => {
debugger;
let before_change = $country.data('val');
console.log(before_change);
const country = $country.val();
});
代碼:3
$country.select2({}).focus(function () {
debugger;
console.log(v.target.value);
// Store the current value on focus and on change
$(this).data('pre', $(this).val());
}).on('change', e => {
debugger;
let before_change = $country.data('pre');
console.log(before_change);
const country = $country.val();
});
代碼 : 4
$country.click(function(v){
debugger;
console.log(v.target.value);
// Store the current value on focus and on change
$(this).data('pre', $(this).val());
}).on('change', e => {
debugger;
let before_change = $country.data('pre');
console.log(before_change);
const country = $country.val();
});
代碼:5
$country.on('focusin', function () {
debugger;
// Store the current value on focus and on change
$(this).data('pre', $(this).val());
}).on('change', e => {
debugger;
var before_change = $(this).data('pre');
const country = $country.val();
});
代碼:6
$country.on('focusin', function () {
debugger;
// Store the current value on focus and on change
$(this).data('pre', $(this).val());
}).on('change', e => {
debugger;
var before_change = $(this).data('pre');
const country = $country.val();
});
在下拉選單的 select2 中,我的焦點/聚焦/點擊事件沒有被呼叫,因此我無法設定以前的值。我希望在手動呼叫下拉串列的更改事件之前我的先前值
注意:在 select2 下拉更改中,我的更改事件被呼叫,但其他事件未被呼叫
uj5u.com熱心網友回復:
在change與選擇2.
焦點/點擊不起作用,因為底層select已被隱藏選擇2 包裝紙。
目前尚不清楚要求,如果您想要“初始值”與“當前值之前的值”,要獲得“前一個值”,您需要在“當前”值更改時更新。
$('.select2').select2();
var $country = $(".select2");
$country.on('change', function () {
var newStatus = $(this).val();
var oldStatus = $(this).data('pre');
// add this line
$country.data('pre', newStatus);
console.log(newStatus "-" oldStatus);
}).data('pre', $country.val());
<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>
<!-- no indication what was in OPs select, so using starter entries -->
<select class="select2">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
</select>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/316752.html
標籤:查询 jquery-ui jquery-select2
上一篇:每一行只勾選一個復選框
