Javascript 如何使用jquery数据比较两个输入前/后值

Javascript 如何使用jquery数据比较两个输入前/后值,javascript,jquery,variables,Javascript,Jquery,Variables,我正在制作一个表格,需要比较输入的值。比如说,我有一个来自服务器的输入,值为name1,然后用户将其更改为name2。如何获得初始值和新值?下面的脚本显示了我解决此问题的尝试,但当我查看控制台输出时,oldAddressNameValue没有旧值,newAddressNameValue也没有。 我做错了什么 var $el = $('#inputAddressName'); var oldAddressNameValue = $el.data('oldVal', $el.val()); cons

我正在制作一个表格,需要比较输入的值。比如说,我有一个来自服务器的输入,值为name1,然后用户将其更改为name2。如何获得初始值和新值?下面的脚本显示了我解决此问题的尝试,但当我查看控制台输出时,oldAddressNameValue没有旧值,newAddressNameValue也没有。 我做错了什么

var $el = $('#inputAddressName');
var oldAddressNameValue = $el.data('oldVal', $el.val());
console.log(oldAddressNameValue);
var newAddressNameValue = $el.change(function () {
    var $this = $(this);
    var newValue = $this.data('newVal', $this.val());
});
console.log(newAddressNameValue);

感谢

数据函数将值分配给属性,但随后将返回用于jquery链接的元素。 如果直接输出$el.data'oldVal',它应该返回值

console.log($el.data('oldVal'))

或者您可以将.data的单参数版本的结果分配给变量。

el.change中的代码在输入更改之前不会执行,因此日志不会记录任何内容,我也不太确定您声明变量的方式。这很有效

var $el = $('#inputAddressName');
var oldAddressNameValue = $el.val(),
    newAddressNameValue;

console.log(oldAddressNameValue);

$el.on('change', function () {
    var $this = $(this);
    $this.data('newVal', $this.val());
    newAddressNameValue = $this.val();
    console.log(newAddressNameValue); 
});

您只是将控制台日志放在了错误的位置,下面的代码显示您的oldVal和newVal都存在。 $document.readyfunction{ var$el=$'inputAddressName'; var oldAddressNameValue=$el.data'oldVal',$el.val; console.logoldAddressNameValue.dataoldVal; var newAddressNameValue=$el.changefunction{ var$this=$this; var newValue=$this.data'newVal',$this.val; console.lognewAddressNameValue.datanewVal; console.lognewAddressNameValue.dataoldVal; }; }; data函数返回jQuery包装的元素。如果要获取实际存储的值,则应使用$el.datakey

以下是您需要做的:

var $el = $('#inputAddressName');
$el.data('oldVal', $el.val());
$el.change(function () {
    var $this = $(this);
    $this.data('newVal', $this.val());
    console.log($this.data('oldVal'));
    console.log($this.data('newVal'));
});

还有。

要在JavaScript中查找的初始值,只需使用defaultValue属性:

var input = document.getElementById('inputAddressName'),
    oldValue = input.defaultValue,
    newValue = input.value;
例如,在jQuery中:

$('#inputAddressName').on('change', function () {
     var el = this,
         oldValue = el.defaultValue,
         newValue = el.value;
});
$'inputAddressName'。在'change'上,函数{ var el=这个, oldValue=el.defaultValue, newValue=el.value; console.logoldValue,newValue; }; 试一试

var$el=$inputAddressName; $el.data{oldVal:$el.val,newVal:void 0}; var oldAddressNameValue=$el.dataoldVal ,newAddressNameValue; $el.onchange,函数{ var$this=$this; $this.datanewVal,$this.val; newAddressNameValue=$this.datanewVal; console.logoldAddressNameValue,newAddressNameValue; };
你在控制台中看到了什么?嗯,我使用的不是完全的控制台,而是chrome源代码。我看到了作用域变量,对于oldAddressNameValue,我看到了m.fn.init[1],但没有看到name1I。当我将console.log放在$el.on…中时,我得到了newAddressNameValue值,但是如果我将它放在它的外部,在底部,变量就不再存在本地/全局作用域。我说得对吗?如果是这样,如何在更改事件之外获取newAddressNameValue?在更改事件发生之前,newAddressNameValue应该是未定义的。console.logoldAddressNameValue,newAddressNameValue,当直接在$el.onchange之后编写时,fn应该立即运行,而不必等待可能的更改事件发生。一旦发生更改事件,console.logoldAddressNameValue、newAddressNameValue;应该同时返回两个值。若要求访问原始帖子以外的其他js的oldAddressNameValue、newAddressNameValue,是否可以发布其他js?