首页 > 解决方案 > 在 Jquery 中记录 $(this).val()

问题描述

我发现了一个在输入字段更改值时记录的小提琴。但是我也想记录新值。我已经尝试了我在网上找到的所有组合,但无法让它发挥作用。这是一个小提琴:https ://jsfiddle.net/Xezmb/8/

指数

<input type="number" id="n" value="5" step=".5"/>

脚本

    $(":input").bind('keyup change click', function (e) {
    if (! $(this).data("previousValue") || 
           $(this).data("previousValue") != $(this).val()
       )
   {
        console.log("changed");           
        $(this).data("previousValue", $(this).val());
   }
        
});


$(":input").each(function () {
    $(this).data("previousValue", $(this).val());
});

标签: javascripthtmljquery

解决方案


您可以通过更改共享代码的第 6 行来做到这一点。

从:

console.log("changed");           

至:

console.log("changed: ", $(this).val() );           

推荐阅读