Resetting a multi-stage form with jQuery

前端 未结 30 2508
谎友^
谎友^ 2020-11-22 00:58

I have a form with a standard reset button coded thusly:


Trouble i

30条回答
  •  暖寄归人
    2020-11-22 01:32

    I made a slight variation of Francis Lewis' nice solution. What his solution doesn't do is set the drop-down selects to blank. (I think when most people want to "clear", they probably want to make all values empty.) This one does it with .find('select').prop("selectedIndex", -1).

    $.fn.clear = function()
    {
        $(this).find('input')
                .filter(':text, :password, :file').val('')
                .end()
                .filter(':checkbox, :radio')
                    .removeAttr('checked')
                .end()
            .end()
        .find('textarea').val('')
            .end()
        .find('select').prop("selectedIndex", -1)
            .find('option:selected').removeAttr('selected')
        ;
        return this;
    };
    

提交回复
热议问题