亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果條件不滿足,則阻止 Select 更改所選選項

如果條件不滿足,則阻止 Select 更改所選選項

慕的地6264312 2021-10-21 10:33:47
我有一個選擇框。目前,此選擇框在 上進行 ajax 調用change?,F在,我只想在滿足條件時撥打電話。所以,這是我的代碼:$('#buildingSelect').on('change', function(){    var result = checkDirtyStatus();    //this checkDirtyStatus alert message if there is some changes on the form.    //if cancel return false, if confirm return true.    if(result === false) {        return;    }    //make ajax call});這可以防止進行 ajax 調用,但是,這會更改選擇的選定選項,即,如果option1在開始時選擇并且如果我嘗試選擇下一個選項,那么它將更改選定的選項,option2然后僅檢查狀態。在互聯網上搜索時,我得到了focusin.$('#buildingSelect').on('focusin', function(){    // console.log("Saving value " + $(this).val());    var result = checkDirtyStatus();    if(result === false) {        return;    }}).on('change', function(){    g_building_id = $(this).val();    getAmenitiesDetails(g_building_id);});但是,focusin無論我單擊cancel還是,使用此選項都會使警報框每次都出現ok。這可能是因為,focusin當我單擊Ok或 時它會再次調用Cancel。檢查此狀態的最佳選項是什么,如果結果為假,我也不想更改所選選項。
查看完整描述

3 回答

?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

最后,通過混合來自 Rory 的鏈接和組織代碼的想法。我已經找到了解決我的問題的方法。所以,如果有人遇到類似的問題,這里是我的解決方案。


$(function(){

    var lastSel;

    $('#buildingSelect').on('focusin', function(){

        lastSel = $("#buildingSelect option:selected");

    }).on('change', function(){

        if(!checkDirtyStatus()) {

            lastSel.prop("selected", true);

            return;

        }else{

            //made ajax call

           //$.ajax({})

        }

    });

});

function checkDirtyStatus(){

        let dirtyStatus = getDirtyStatus();

        if(dirtyStatus){

            return confirm("Changes you made may not be saved.");

        }

        return true;

    }


查看完整回答
反對 回復 2021-10-21
?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

讓我們看看你的功能:


function checkDirtyStatus(){

  dirtyStatus = true; // I assume this is only for testing

  if(dirtyStatus === true){ // This can be simplified.

    if (confirm("Changes you made may not be saved.")) {

      return true;

    }else{

      return false;

    }

  }

}

確認返回一個布爾值,它要么是真要么是假,所以你可以像這樣簡化你的函數:


function checkDirtyStatus(){

  dirtyStatus = true;

  if(dirtyStatus){

    return confirm("Changes you made may not be saved.");

  }

  // Notice that you do not return anything here. That means that

  // the function will return undefined.

}

您的其他功能可以這樣簡化:


$('#buildingSelect').on('change', function(){

  if(!checkDirtyStatus()){

    // Here you probably want to set the value of the select-element to the

    // last valid state. I don't know if you have saved it somewhere.

    return; 

  }


  //make ajax call

});


查看完整回答
反對 回復 2021-10-21
?
www說

TA貢獻1775條經驗 獲得超8個贊

我玩過你的 codepen,你的選擇器有一些錯誤。當我對您的解釋感到困惑時,我將嘗試解釋您可以更新的內容以及如何在您的代碼中使用它,我希望這是您解決問題所需要的。


首先,我會將您的 js 更改為:


var lastSel = $("#buildingSelect").val();

$("#buildingSelect").on("change", function(){

  if ($(this).val()==="2") {

    $(this).val(lastSel);

    return false;

  }

});

在 jquery 中獲取選擇框值的正確方法是使用.val(). 在您的情況下,您選擇了整個選定的選項元素。我將此值存儲在lastSel變量中。然后在更改函數中,選擇列表的新值是$(this).val()。我檢查這個值,如果它等于 2,我將它恢復為存儲在 lastSel 變量中的值$(this).val(lastSel)。請記住,選擇列表的值始終是字符串,如果要檢查數字,必須首先將其轉換為數值,例如使用 parseInt。


如果您想使用 checkDirtyStatus 進行檢查,那么您應該只在更改中調用此函數并將 lastSel 和 newSel 作為參數傳遞,如下所示:


$("#buildingSelect").on("change", function(){

  checkDirtyStatus(lastSel, $(this).val());

});

然后,您可以將更改函數中的邏輯轉移到 checkDirtyStatus 函數中,并在那里進行檢查。在這種情況下,如果您希望恢復選擇值而不是$(this).val(lastSel)您將執行$("#buildingSelect").val(lastSel).


我希望這有幫助。


查看完整回答
反對 回復 2021-10-21
  • 3 回答
  • 0 關注
  • 520 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號