2017-07-06 4 views
0

wie vorherige Wert von Textbox in jquery auf Änderungsereignis

$(".whitebox").on('change', function() { 
 
     var previousAmt = $(this).data("old"); // i want previous value 
 
     var id = this.id; 
 
     var slpitid = id.split("_"); 
 

 
     var checkboxid = "#chck_" + slpitid[0]; 
 
     if ($(checkboxid).is(':checked')) { 
 
      var tipFlag = true; 
 
     } 
 
     else { 
 
      var tipFlag = false; 
 
     } 
 

 
     if (tipFlag === true) 
 
     { 
 
      $("#amounttipchange").modal('show'); 
 
     } 
 
     //alert(previousAmount); 
 
    });

i vorherigen Wert speichern auf Änderungsereignis erhalten möchten, wie kann ich in jquery durchführen i nicht in der Lage bin Wert speichern $ (this mit) .data ("alt");

$("#checkboxid").on('click',function(){ 
    $(this).data('old',$(this).val()); 
}); 

und dann Änderungsereignis verwenden: Jede Hilfe wird

+0

Bitte fügen Sie Ihre 'HTML' Code zu – Ionut

+0

' ich bin nicht in der Lage, Wert mit $ (this) .data ("alt") zu speichern - warum? –

+0

previousAmt = $ (this) .data ("alt") Ausgabe ist emptty – user6885473

Antwort

1

Sie haben fast dieses Recht, aber Sie vergessen, den data-old in Ihrem Code zu aktualisieren, ist unten ein Beispiel und jsbin

$(".whitebox").on('change', function (event) { 
    // on first change will be empty string 
    // as no previous value was set 
    var oldValue = event.target.dataset.old; 
    console.log('old value', oldValue); 

    // your code here 

    // at the end assign new value to data-old attribute 
    $(this).data('old', event.target.value); 
    console.log(event.target.value); 
}); 

http://jsbin.com/jasaqiwuwo/

0

var prevVal = $('input').val(); //init val 
 
var valuesHistory = [prevVal]; 
 
$('input').data('prevData', prevVal); 
 

 
$('input').on('change', function() { 
 
    console.log('prev value from data: ', $(this).data('prevData')); 
 
    console.log('prev value from var: ', prevVal); 
 
    console.log('log: ', valuesHistory); 
 
    valuesHistory.push($('input').val()); 
 
    $('input').data('prevData', $('input').val()); 
 
    prevVal = $(this).val(); 
 
}); 
 
// you can also get the prev value, like that valuesHistory[valuesHistory.length-2];
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text">

0

Versuchen Sie dies geschätzt.

Verwandte Themen