admin管理员组文章数量:1418103
I have a popup form with 4 textboxes, all of which have default values when the page loads.
Here's the scenario... The user has filled in the form with their details but decides not to submit the form and closes the popup. I want the values of the textboxes to revert to their default values when the user close the popup.
Here's my code so far.
$(".close").click( function() {
$(".popup").find("input").val("Default value of textbox");
});
Rather than inserting "Default value of textbox" as the value, I want it to be the actual default value.
I have a popup form with 4 textboxes, all of which have default values when the page loads.
Here's the scenario... The user has filled in the form with their details but decides not to submit the form and closes the popup. I want the values of the textboxes to revert to their default values when the user close the popup.
Here's my code so far.
$(".close").click( function() {
$(".popup").find("input").val("Default value of textbox");
});
Rather than inserting "Default value of textbox" as the value, I want it to be the actual default value.
Share Improve this question asked Nov 4, 2011 at 12:54 TomTom 13k50 gold badges153 silver badges247 bronze badges 2- 2 isn't this what <input type='reset' /> is for? – kͩeͣmͮpͥ ͩ Commented Nov 4, 2011 at 12:59
- you could use the form inputs "defaultValue" property ? appnovation./blog/… – cyberjar09 Commented Feb 27, 2015 at 2:18
3 Answers
Reset to default 4You could put the default values in a 'data attribute' on the elements themselves...I'll create a fiddle to show this... As seen in this jsFiddle
I should also note that you can put the data attribute on the elements from the server...I'm just showing how you could do it all from the client...
// First store all the default values
var def_vals = [];
jQuery('#popup input[type="text"]').each(
function () {
def_vals[jQuery(this).attr('name')] = jQuery(this).val();
}
);
alert(def_vals['test']);
// Restore defaults on close
jQuery('#popup .closeButton').click( function () {
jQuery('#popup input[type=text]').each(
function() {
jQuery(this).val(def_vals[jQuery(this).attr('name')]);
}
);
});
By way of closure
// add to the place that opens the popup
$(".popup input").one(function(){
var value = $(this).val();
var that = this;
$(".close").one(function(){
$(that).val(value);
});
});
本文标签: javascriptJquery revert textboxes back to their original valuesStack Overflow
版权声明:本文标题:javascript - Jquery revert textboxes back to their original values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745271982a2650949.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论