我需要重置我的页面的所有字段元素的值。这些元素是:输入、选择(组合框)、复选框和单选组。 搜索发现如下代码:
$("*").attr('value', '');
$("input[type='checkbox']").attr("checked",false);
$('select').each(function() {
if($(this).children().length > 0) {
$($(this).children()[0]).attr('selected', 'selected');
$(this).change();
}
});
此代码的输入和复选框正常,但其他组件在测试的代码中存在问题。 radio 组需要或采取任何类型的选择或至少选择第一个。组合框使用此代码重置,但当尝试选择新值时,它不会存储您的值。
谢谢!
最佳答案
非文本输入需要刷新。下面是这样做的一个示例:
//reset text input values, then refresh slider widgets
$(".ui-input-text").val('').filter('.ui-slider-input').slider('refresh');
//reset checkboxes and radio inputs, then refresh them
$(".ui-checkbox input[type='checkbox'], .ui-radio input[type='radio']").prop("checked", false).checkboxradio("refresh");
//reset all select menus and then refresh them
$('.ui-select select').val('').selectmenu('refresh');
return false;
这是一个演示:http://jsfiddle.net/MLewd/1/
关于forms - 从 jQuery Mobile 重置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13765644/