Javascript JQuery:获取原始输入值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8490446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery: Get original input value
提问by My Head Hurts
Is it possible to get the initial value of a form input field?
是否可以获取表单输入字段的初始值?
For example:
例如:
<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"
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.
但是,如果用户随后将#one
输入字段的内容更改为 2,则 html 看起来仍然相同(以 1 作为值),但上面的 JQuery 调用将返回 2。
JSFiddle to play with: http://jsfiddle.net/WRKHk/
JSFiddle玩:http: //jsfiddle.net/WRKHk/
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.
我认为这仍然可以获取原始值,因为我们可以调用document.forms["formId"].reset()
将表单重置为其原始状态。
采纳答案by dxh
The DOM element has the attribute "value" which you can access by:
DOM 元素具有属性“value”,您可以通过以下方式访问它:
element.getAttribute('value');
... which may be different from the element's propertyvalue
.
...这可能与元素的属性不同value
。
Note that you may call .setAttribute('value', 'new value')
which will actually affect any form.reset()
that is called after.
请注意,您可能会调用.setAttribute('value', 'new value')
which 实际上会影响form.reset()
之后调用的任何内容。
回答by graphicdivine
Try this:
尝试这个:
$("#one")[0].defaultValue;
回答by Shadow Wizard is Ear For You
Input element got property called defaultValue
that is storing the original value.
输入元素得到了调用的属性defaultValue
,用于存储原始值。
To reach it via jQuery, just use .prop()
:
要通过 jQuery 访问它,只需使用.prop()
:
var value = $(this).prop("defaultValue");
Updated jsFiddle- after changing to "two", the above will return "one".
更新了 jsFiddle- 更改为“二”后,上面将返回“一”。
回答by techfoobar
The HTML does change. Just that the browser's default view source tool always shows you the original source fetched from server. If you use something like Firebug, you'll be able to see the dynamic changes in HTML.
HTML 确实发生了变化。只是浏览器的默认查看源工具总是向您显示从服务器获取的原始源。如果您使用 Firebug 之类的工具,您将能够看到 HTML 中的动态变化。
One way of getting the initial value is by storing it in an additional attribute in addition to 'value'. For example, if you serve out your HTML like:
获取初始值的一种方法是将其存储在“值”之外的附加属性中。例如,如果您像这样提供 HTML:
<input type="text" name="one" value="one" origvalue="one" id="one" />
You can always get back the original value by calling:
您始终可以通过调用来取回原始值:
$("#one").attr("origvalue");
回答by Tom Maeckelberghe
Save the original_values @ document.ready.
$(function(){
var original_values = $.map($('form input'), function(input){ return $(input).val() });
})
回答by will
You could try storing the value in data. For example:
您可以尝试将值存储在数据中。例如:
$('#one').data('val',$('#one').val());
And then you could easily retrieve it later like this:
然后你可以像这样轻松检索它:
console.log($('#one').data('val'));
回答by bondythegreat
var initialVal = $('#one').val();
alert(initialVal);
$('#one').live("focusout", function() {
var attribute = $("#one").attr("value");
var value = $(this).val();
var get = $(this).get(0).value;
alert( "Attribute: " + attribute + "\n\nValue: " + value + "\n\ninit val: " + initialVal +"\n\nGet: " + get );
});