how to prevent to change selectbox value if i say no to confirm

后端 未结 4 1454
野趣味
野趣味 2021-02-07 00:59

i m using a select box of country, when user select a country then add branch link appears and user add branches under that country, but when user want to change country then al

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 01:28

    You can just store the previous value and set it back if needed, like this:

    var countryVal;
    $("#country").change(function() {
      var newVal = $(this).val();
      if (!confirm("Are you sure you wish to destroy these country branches?")) {
        $(this).val(countryVal); //set back
        return;                  //abort!
      }
      //destroy branches
      countryVal = newVal;       //store new value for next time
    });
    

    Or use .data() as @Gaby suggests, like this:

    $("#country").change(function() {
      var newVal = $(this).val();
      if (!confirm("Are you sure you wish to destroy these country branches?")) {
        $(this).val($.data(this, 'val')); //set back
        return;                           //abort!
      }
      //destroy branches
      $.data(this, 'val', newVal);        //store new value for next time
    });
    

提交回复
热议问题