我無法獲得下拉的前一個值
。下面是下拉選單的設計部分
下面是我為獲得前一個值而實作的多段代碼
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'/span>, 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) 。
//在焦點和變化時存盤當前值。
$(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) 。
//在焦點和變化時存盤當前值。
$(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'/span>, function () {
debugger;
//在焦點和變化時存盤當前值。
$(this)。 data('pre', $(this).val()。
}).on('change', e => {
debugger。
var before_change = $(this).data('pre)。
const country = $country.val()。
});
代碼: 6
$country.on('focusin'/span>, function () {
debugger;
//在焦點和變化時存盤當前值。
$(this)。 data('pre', $(this).val()。
}).on('change', e => {
debugger。
var before_change = $(this).data('pre)。
const country = $country.val()。
});
在選擇下拉選單時,我的焦點/聚焦/點擊事件沒有被呼叫,因此我無法設定之前的值。 我希望只有在手動呼叫下拉選單的更改事件時才能設定前一個值
。注意:在下拉變化時,我的變化事件被呼叫,但其他事件沒有被呼叫
。uj5u.com熱心網友回復:
在change上與select2一起作業。
聚焦/點擊不起作用,因為底層的選擇已經被select2包裝器隱藏。
這個要求不明確,如果你想要 "初始值 "與 "當前值之前的值",要獲得 "之前的值",你需要在 "當前 "值改變時進行更新。
。$('.select2').select2()。
var $country = $(".select2") 。
$country.on('change', function () {
var newStatus = $(this).val()。
var oldStatus = $(this).data('pre)。
//加入這行
$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="styleheet" />
<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>/span>
<!--沒有說明OPs選擇中的內容,所以使用啟動項-->/span>
<select class="select2"/span>>
<option value="AL"/span>> 阿拉巴馬州</option>
<option value="AK" > 阿拉斯加</選項>/span>
<option value="AZ"/span>> 亞利桑那州</option>
</select>/span>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/319897.html
標籤:
