Getting value of select (dropdown) before change

后端 未结 18 2459
无人共我
无人共我 2020-11-22 08:56

The thing I want to achieve is whenever the

提交评论

  • 2020-11-22 08:58

    There are several ways to achieve your desired result, this my humble way of doing it:

    Let the element hold its previous value, so add an attribute 'previousValue'.

    <select id="mySelect" previousValue=""></select>
    

    Once initialized, 'previousValue' could now be used as an attribute. In JS, to access the previousValue of this select:

    $("#mySelect").change(function() {console.log($(this).attr('previousValue'));.....; $(this).attr('previousValue', this.value);}
    

    After you are done using 'previousValue', update the attribute to current value.

    0 讨论(0)
  • 2020-11-22 08:58

    I needed to reveal a different div based on the selection

    This is how you can do it with jquery and es6 syntax

    HTML

    <select class="reveal">
        <option disabled selected value>Select option</option>
        <option value="value1" data-target="#target-1" >Option 1</option>
        <option value="value2" data-target="#target-2" >Option 2</option>
    </select>
    <div id="target-1" style="display: none">
        option 1
    </div>
    <div id="target-2" style="display: none">
        option 2
    </div>
    

    JS

    $('select.reveal').each((i, element)=>{
        //create reference variable 
        let $option = $('option:selected', element)
        $(element).on('change', event => {
            //get the current select element
            let selector = event.currentTarget
            //hide previously selected target
            if(typeof $option.data('target') !== 'undefined'){
                $($option.data('target')).hide()
            }
            //set new target id
            $option = $('option:selected', selector)
            //show new target
            if(typeof $option.data('target') !== 'undefined'){
                $($option.data('target')).show()
            }
        })
    })
    
    0 讨论(0)
  • 2020-11-22 08:59

    Track the value by hand.

    var selects = jQuery("select.track_me");
    
    selects.each(function (i, element) {
      var select = jQuery(element);
      var previousValue = select.val();
      select.bind("change", function () {
        var currentValue = select.val();
    
        // Use currentValue and previousValue
        // ...
    
        previousValue = currentValue;
      });
    });
    
    0 讨论(0)
  • 2020-11-22 08:59

    keep the currently selected drop down value with chosen jquery in a global variable before writing the drop down 'on change' action function. If you want to set previous value in the function you can use the global variable.

    //global variable
    var previousValue=$("#dropDownList").val();
    $("#dropDownList").change(function () {
    BootstrapDialog.confirm(' Are you sure you want to continue?',
      function (result) {
      if (result) {
         return true;
      } else {
          $("#dropDownList").val(previousValue).trigger('chosen:updated');  
         return false;
             }
      });
    });
    
    0 讨论(0)
  • 2020-11-22 09:05

    please don't use a global var for this - store the prev value at the data here is an example: http://jsbin.com/uqupu3/2/edit

    the code for ref:

    $(document).ready(function(){
      var sel = $("#sel");
      sel.data("prev",sel.val());
    
      sel.change(function(data){
         var jqThis = $(this);
         alert(jqThis.data("prev"));
         jqThis.data("prev",jqThis.val());
      });
    });
    

    just saw that you have many selects on page - this approach will also work for you since for each select you will store the prev value on the data of the select

    0 讨论(0)
  • 提交回复
    热议问题