我想查找所有表單元素,並指定一個類,並檢查它們是否已被修改。 if語句確實是工作,但我不知道要糾正這個問題。這是正確的方向還是我弄錯了?如何找到jQuery中尚未更改的輸入元素?
$("input").find(".formWithMyClass").load(function() {
if (this.value !== this.defaultValue){
$(this).addClass("inactiveForm");
}
});
乾杯!
我想查找所有表單元素,並指定一個類,並檢查它們是否已被修改。 if語句確實是工作,但我不知道要糾正這個問題。這是正確的方向還是我弄錯了?如何找到jQuery中尚未更改的輸入元素?
$("input").find(".formWithMyClass").load(function() {
if (this.value !== this.defaultValue){
$(this).addClass("inactiveForm");
}
});
乾杯!
你可以寫一個自定義的選擇...
(function($) {
$.expr[':'].defaultValue = function(obj) {
return obj.value === obj.defaultValue;
};
})(jQuery);
那麼你應該能夠做到$('input:defaultValue')
讓所有input
元素其中的value
屬性設置爲defaultValue
。
但是,如果您想要查找所有輸入類型元素,則可能需要擴展該自定義選擇器。
我似乎無法找到獲得select
元素的原始值(並非沒有首先設置它們)的方法。他們的defaultValue
財產是undefined
和selectedIndex
可能是最後選擇的值。
$(function() {
$('form.formWithMyClass input').each(function() {
if (this.value !== this.defaultValue) {
$(this).closest('form').addClass("inactiveForm");
}
});
});
如果你在它有一個<form class="formWithMyClass">
與<input />
秒。
$(function() {
$('input.formWithMyClass').each(function() {
if (this.value !== this.defaultValue) {
$(this).closest('form').addClass("inactiveForm");
}
});
});
如果你在它有一個<form>
與<input class="formWithMyClass" />
秒。
您還需要了解的東西就像選擇菜單等等。JQuery的API中 看.VAL(值)
+1優雅的解決方案。如果OP想要輕鬆檢查*存在任何*改變的字段,則可以使用反碼。 – jensgram 2010-11-25 17:09:08