有没有办法将select重置为使用jquery在加载时选择的选项

时间:2012-01-09 21:42:39

标签: jquery html dom

有没有办法将select重置为使用jquery在加载时选择的选项,而无需创建初始值的自定义缓存?

2 个答案:

答案 0 :(得分:1)

编辑:根据您的评论,更新代码以创建自定义attr到select元素并使用它们进行重置。请在此处 DEMO

//cache all the initial values of the drop down in a custom attr.
$('select').each (function (index) {
    $(this).attr('initial-value', $(this).val());
});

//reset is the button that will be used to reset the value of 
//all the drop down to its initial value.
$('#reset').click (function () {
    $('select').each (function (index) {
        $(this).val($(this).attr('initial-value'));
    });
});

以下是旧帖子,它使用简单的javascript变量来保存初始值,

您可以将初始值缓存在简单缓存中,然后使用它们进行重置。

在此处查看 DEMO

答案 1 :(得分:0)

form.reset方法,但它也会重置其他形式的元素。

否则将一些类放在选项上以将该选项标识为默认选项。

相关问题