如何在onchange之前从下拉列表中获取先前选定的值

时间:2018-02-08 06:43:52

标签: javascript jquery javascript-events

我知道这是最简单的问题,但无法得到正确的答案

<div class="col-12 col-md-4 col-lg-5 mt10-xs">
     <p>Status</p>
     <div class="form-group FL mr20 w100-xs">
           <div class="rsi-custom-select">
                 <select class="form-control" id="statustab_{{ $row['placementkey'] }}" class="select2-selecting" onchange="listingByStatus(this,'{{ $row['placementkey'] }}')">
                     @foreach($placementStatus as $pl)
                        <option @if($pl['placement_status_id'] ==  $row['statusid'] ) selected @endif value="{{$pl['placement_status_key']}}">{{$pl['placement_status']}}</option>
                     @endforeach
                 </select>
            </div>
      </div>
</div>

这是我的onchange功能。在此如何从选择框中获取先前的选定值

function listingByStatus(ths,key){
      var thisSelect = $('#statustab_'+key).text();
      var statusText = $( "#statustab_"+key+" option:selected" ).text();
      var currentval = $( "#statustab_"+key+" option:selected" ).val();
}

5 个答案:

答案 0 :(得分:6)

当元素获得焦点时,使用data()保存原始值:

$('.select2-selecting').on('focusin', function(){
    console.log("Saving value " + $(this).val());
    $(this).data('val', $(this).val());
});

然后在onchange函数中获取已保存的旧值:

function listingByStatus(ths,key){
         var thisSelect = $('#statustab_'+key).text();
         var statusText = $( "#statustab_"+key+" option:selected" ).text();
         var currentval = $( "#statustab_"+key+" option:selected" ).val();
         var prev = $('.select2-selecting').data('val'); //old value
         console.log("Old value " + prev);
}

答案 1 :(得分:5)

您可以在页面上添加隐藏的输入字段

<input type="hidden" id="previous_value" name"previous_value" value="abc"/>

然后在您的脚本中,您可以访问任何其他输入:

var previous = $('#previous_value').val();

在发生onchange事件后设置:

$('#statustab_{{ $row['placementkey'] }}').addEventListener('change', function (e) {
    $('$previous_value').val(e.value)
}) 

答案 2 :(得分:2)

  1. 使用data-*存储旧值
  2. data-*作为旧值,将所选选项作为新值
  3. &#13;
    &#13;
    $('select').change(function(){
    var oldvar = $(this).attr('data-old') !== typeof undefined? $(this).attr('data-old') :"";
    var newval = $("option:selected",this).text();
    $(this).attr('data-old',newval)
    console.log("prev value :" + oldvar)
    console.log("curr value :" + newval)
    }).change();
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    </select>
    &#13;
    &#13;
    &#13;

答案 3 :(得分:1)

focus事件与更改事件相结合,以实现您的目标:

试试这样:

&#13;
&#13;
(function () {
    var previous;

    $("select[name=test]").focus(function () {
        // Store the current value on focus, before it changes
        previous = this.value;
    }).change(function() {
        // Do soomething with the previous value after the change
        document.getElementById("oldData").innerHTML = "<b>Previous: </b>"+previous;
        
        previous = this.value;
    });
})();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<select name="test">
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
    <option value="four">four</option>
</select>
<div id="oldData"></div>
&#13;
&#13;
&#13;

这将为您提供所选的最后一个值。

希望这能帮到你!

答案 4 :(得分:1)

尝试以下代码。希望它会对你有所帮助。

&#13;
&#13;
$('select').change(function(){
  var oldval = $(this).prop('data-old') !== undefined ? $(this).prop('data-old') : "";
  var newval = $("option:selected",this).text();
  console.log("previous value : " + oldval);
  console.log("current value : " + newval);
  $(this).prop('data-old',newval);
}).change();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option>Apple</option>
  <option>Mango</option>
  <option>Banana</option>
  <option>Orange</option>
</select>
&#13;
&#13;
&#13;