jQuery:获取原始输入值 [英] JQuery: Get original input value
问题描述
是否可以获取表单输入字段的初始值?
Is it possible to get the initial value of a form input field?
例如:
<form action="" id="formId">
<input type="text" name="one" value="one" id="one" />
</form>
$("#one").attr("value"); //would output "one"
$("#one").val(); //would output "one"
$("#one").get(0).value; //would output "one"
但是,如果用户随后将#one
输入字段的内容更改为两个,则html仍将看起来相同(值为1),但是上面的JQuery调用将返回两个.
However, if the user then changed the content of the #one
input field to two, the html would still look the same (with one as the value) but the JQuery calls above would return two.
与JSFiddle一起玩: http://jsfiddle.net/WRKHk/
我认为仍然必须能够获得原始值,因为我们能够调用document.forms["formId"].reset()
将表格重置为原始状态.
I think that this must still be possible to get the orginal value, since we are able to call document.forms["formId"].reset()
to reset the form to its original state.
推荐答案
DOM元素具有值"属性,您可以通过以下方式访问该属性:
The DOM element has the attribute "value" which you can access by:
element.getAttribute('value');
...可能与元素的属性 value
不同.
... which may be different from the element's property value
.
请注意,您可以调用.setAttribute('value', 'new value')
,这实际上会影响之后调用的任何form.reset()
.
Note that you may call .setAttribute('value', 'new value')
which will actually affect any form.reset()
that is called after.
这篇关于jQuery:获取原始输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!