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

javascript - Find default value of field after page refresh - Stack Overflow

programmeradmin1浏览0评论

I'm looking for a way, with jQuery, to get the default value of an input field (in an HTML field).

Seems simple enough. Just calling $("#id-of-field").attr('value') should return the value I'm wanting right?

Thing is, if the value is Foo, when I load the page, it returns Foo. No problem there. But if I write Bar in the field, then reload (not submit) the page, $("#id-of-field").attr('value') will return me Bar, even if in the source code, the value attribute of the field is still Foo.

That makes validation of my form sticky, because when I get my default values to ignore them if the field has not been filled, I could get a real value in the mix of "Values to be ignored".

FYI, I can't put in the values manually, since the form is dynamic, and cannot query directly the database because it's a Wordpress Website.

I'm looking for a way, with jQuery, to get the default value of an input field (in an HTML field).

Seems simple enough. Just calling $("#id-of-field").attr('value') should return the value I'm wanting right?

Thing is, if the value is Foo, when I load the page, it returns Foo. No problem there. But if I write Bar in the field, then reload (not submit) the page, $("#id-of-field").attr('value') will return me Bar, even if in the source code, the value attribute of the field is still Foo.

That makes validation of my form sticky, because when I get my default values to ignore them if the field has not been filled, I could get a real value in the mix of "Values to be ignored".

FYI, I can't put in the values manually, since the form is dynamic, and cannot query directly the database because it's a Wordpress Website.

Share Improve this question edited Nov 23, 2011 at 19:02 César 10.1k6 gold badges55 silver badges75 bronze badges asked Nov 23, 2011 at 18:44 Fredy31Fredy31 2,8356 gold badges36 silver badges61 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Input fields have the defaultValue DOM property that does exactly what you need.

According to this answer, the jQuery (> 1.6.3) way to access it is

$('#element').prop('defaultValue')

Alternatively, in pure JavaScript, e.g. -

document.getElementById("element").defaultValue

Try this

$('inputSelector').prop('defaultValue');

You could try -

$("#id-of-field")[0].defaultValue

That should get the DOM defaultValue property which should give you the original value of the textbox

https://developer.mozilla/en/XUL/textbox#p-defaultValue

发布评论

评论列表(0)

  1. 暂无评论