我正在阅读answer to this question ,但它只用一个字段解决了问题,我如何查看表单上的所有字段(输入、选择、单选等...)?
这是我目前拥有的(我正在使用委托(delegate),此表单动态注入(inject)到页面):
$('#preferencesBody').delegate('input[type="radio"], select', 'change', function() {
var key = $(this).attr('data-key');
var value = $(this).val();
// How to get the old value, and compare with the current value, if they are different, call saveUserPreferences?
Preferences.saveUserPreferences(key, value);
});
谢谢。
最佳答案
$('#formID :input')
将选择带有 id=formID
的表单上的所有输入字段(包括文本区域和按钮)
所以你的函数看起来像这样..
$('#preferencesBody').delegate(':input', 'change', function() {
var key = $(this).attr('data-key');
var value = $(this).val();
Preferences.saveUserPreferences(key, value);
});
关于javascript - jQuery - 在表单提交时比较所有字段(输入、选择)的新值与旧值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6779464/