我有以下input fields
<input type="hidden" name="field1" id="field1" value="">
<input type="input" name="field2" id="field2" value="">
我做了一些操作,为它们赋值
$("#fiedl1").val("value1");
$("#fiedl2").val("value2");
然后我想在我想将它们重置为原始值
时获取它们的DEFAULT值$("#fiedl1").val( $("#field1").prop("defaultValue") );
$("#fiedl2").val( $("#field2").prop("defaultValue") );
但是,field1
仍保留指定的值value1
,但field2
的默认值为""
。似乎hidden
字段无法设置为其defaultValue?
答案 0 :(得分:1)
执行此操作的简单方法是在html输入标记中添加-q, --quiet
If specified once, the program will be less verbose. Specifically, it will not state when
it has completed establishing all inotify watches. If specified twice, the
program will output nothing at all, except in the case of fatal errors.
-m, --monitor
Instead of exiting after receiving a single event, execute indefinitely. The default
behaviour is to exit after the first event occurs.
-e <event>, --event <event>
Listen for specific event(s) only.
close_write
A watched file or a file within a watched directory was closed, after being opened in
writeable mode. This does not necessarily imply the file was written to.
属性:
data-default
使用<input type="hidden" name="field1" id="field1" value="" data-default="150">
<input type="input" name="field2" id="field2" value="" data-default="150">
获取值:
.data
答案 1 :(得分:1)
在W3 spec:
DOMString类型的defaultValue 当元素的type属性具有值&#34; text&#34;,&#34; file&#34;或&#34;密码&#34;,这表示元素的HTML值属性。如果交互式用户代理中相应表单控件的内容发生更改,则此属性的值不会更改。请参阅HTML 4.01中的value属性定义。
不幸的是,它不适用于隐藏的输入,而是store and retrieve the value from the element data。
// Do this on ready
$("#field1").data("defaultValue", $("#field1").val());
$("#field2").data("defaultValue", $("#field2").val());
// Update the values
$("#field1").val("value1");
$("#field2").val("value2");
console.log($("#field1").val(), $("#field2").val());
// Restore the defaults
$("#field1").val($("#field1").data("defaultValue"));
$("#field2").val($("#field2").data("defaultValue"));
console.log($("#field1").val(), $("#field2").val());
答案 2 :(得分:0)
一种可能的解决方案是,在页面加载时保存全局变量中的默认值,并使用该变量在任意位置设置这些字段。