最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - A JQUERY standard way of storing the original value of a form field? - Stack Overflow

programmeradmin2浏览0评论

Is there a standard way in JQUERY to (at document ready time) store the original values of the form fields on the page in order to check (later) whether the fields have truly changed or not?

Normally I will do something like this:

var NameField = $("INPUT[name='NameField']");
//Record the original value.
NameField.OriginalVal = NameField.val();

This is simple enough, but I'm wondering of there is a "Standard" way to do it.

EDIT

One added requirement that I forgot is that it needs to work for all types of form fields including select and textareas.

Is there a standard way in JQUERY to (at document ready time) store the original values of the form fields on the page in order to check (later) whether the fields have truly changed or not?

Normally I will do something like this:

var NameField = $("INPUT[name='NameField']");
//Record the original value.
NameField.OriginalVal = NameField.val();

This is simple enough, but I'm wondering of there is a "Standard" way to do it.

EDIT

One added requirement that I forgot is that it needs to work for all types of form fields including select and textareas.

Share Improve this question edited May 12, 2010 at 11:26 Tom Hubbard asked May 12, 2010 at 11:00 Tom HubbardTom Hubbard 16.1k14 gold badges61 silver badges87 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 13

The default value (i.e. the value that was specified in the source code using the value attribute) is available automatically as the defaultValue property.

alert($("#myInput").defaultValue);

More info at W3Schools

This seems to be part of the JS spec since 1.0 so it's safe to use. (Reference, German only sorry)

There is no standard way to store original values.

jQuery does have a better way to store values than your example:

var NameField = $("INPUT[name='NameField']");
NameField.data('OriginalVal', NameField.val());

Another simple way is to set a boolean variable on input control's onchange event.

i.e. var isFormValueChanged = false;

$("INPUT[name='NameField']").bind("change", function()
{
       isFormValueCanged = true;
});
发布评论

评论列表(0)

  1. 暂无评论