使用 jQuery 设置输入的实际值属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1634676/
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
Set the actual value attribute of an input with jQuery
提问by Ryan
I am trying to reset the actual value attribute of an input so that it can be reset back to that value if reset, not necessarily the original value when the form was loaded. The problem is these changes don't seem to take effect. I have tried both:
我正在尝试重置输入的实际值属性,以便在重置时可以将其重置回该值,而不一定是加载表单时的原始值。问题是这些更改似乎没有生效。我都试过:
$(this).attr('value', $(this).val());
as well as....
也....
$(this).val($(this).val());
Neither of these actually change the value=""
attribute of the input, which is what is used when a form is reset. The problem is that if someone saves a form (via AJAX), then again edits it and presses reset (w/out saving this time), it will go back to the original values the page loaded with, NOT the values last saved with, which is what I'm trying to get. I know there are ways around this (store in local variables or something), but a jQuery solution would be much better. Any ideas??
这些实际上都不会更改value=""
输入的属性,这是重置表单时使用的属性。问题是,如果有人保存了一个表单(通过 AJAX),然后再次编辑它并按下重置(这次不保存),它将返回到页面加载的原始值,而不是上次保存的值,这就是我想要得到的。我知道有办法解决这个问题(存储在局部变量或其他东西中),但 jQuery 解决方案会好得多。有任何想法吗??
采纳答案by Daniel A. White
when you are doing something like this:
当你做这样的事情时:
$(this).val($(this).val());
it really translates as:
它真的翻译为:
var temp = $(this).val());
$(this).val(temp);
So in essence, its just putting the same value back in.
所以本质上,它只是把相同的值放回去。
What you will want to do is on the document load or the AJAX request, store the value in a variable and then retrieve it when the form is reset.
您要做的是在文档加载或 AJAX 请求中,将值存储在变量中,然后在表单重置时检索它。
回答by andrefy
That sounds like a cool trick, here's another one.
这听起来很酷,这是另一个。
You can modify the DOM by replacing the whole element, so instead of:
您可以通过替换整个元素来修改 DOM,而不是:
$('#my_input').val("new_value_here");
you can use:
您可以使用:
$('#my_input').replaceWith(' < input type="text" value="new_value_here" />');
回答by Ryan
I've come up with a solution for this...
我已经想出了一个解决方案......
When saving a form (via AJAX, on the success callback):
保存表单时(通过 AJAX,在成功回调中):
// Set 'savedValue' attr on elements (for when reset, will reset to last saved values)
$('#myForm input').each(function() {
$(this).attr('savedValue', $(this).val());
});
When needing to reset the form:
当需要重置表单时:
// Reset form
if($('#myForm input:first').attr('savedValue')!=undefined) {
// Have saved values, reset to last saved values
$('#myForm input').each(function() {
$(this).val($(this).attr('savedValue'));
});
}
else {
// No saved values, reset form
$('#myForm')[0].reset();
}
This works great for textboxes, and with a little tweaking you could get it to work with any input type. I've got a lot of inputs so I wanted to avoid manually storing/resetting them if at all possible. This would be a good plugin... maybe tomorrow
这对文本框很有用,只需稍加调整,您就可以使其适用于任何输入类型。我有很多输入,所以我想尽可能避免手动存储/重置它们。这将是一个很好的插件......也许明天
回答by Kevin Gorski
Even better than a local variable, you could use the jQuery Data methods to store the reset values along with the element to be reset.
甚至比局部变量更好,您可以使用 jQuery Data 方法将重置值与要重置的元素一起存储。
When saving:
保存时:
$(this).data("ResetValue", $(this).val());
On reset:
复位时:
$(this).val($(this).data("ResetValue"));
Changing the selectors appropriately, and probably adding the undefined check as Ryan mentioned.
适当地更改选择器,并可能添加瑞安提到的未定义检查。
回答by Gordon Gustafson
Just use thing.value="foo";
Check if it works when thing is a jQuery object as well as just a regular DOM object. using attr('value', $(this).val())
won't work how you want it to
thing.value="foo";
当事物是一个 jQuery 对象以及一个普通的 DOM 对象时,只需使用检查它是否有效。使用attr('value', $(this).val())
不会像你想要的那样工作