Is it possible to clear a form and reset (reload) the page with one button?

前端 未结 4 1223
一生所求
一生所求 2020-12-31 08:11

I\'ve got a form on a page where the user types something in and then a result is returned and displayed on the page. Is it possible for me to have a button that will both,

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 08:29

    I'm a fan of @MikeyHogarth's suggestion since it's called regardless of how the page is refreshed. This is one of those rare times that I find straight javascript to be simpler than jquery so I just wanted to add the code for that.

    $(document).ready(function () {
        resetForms();
    });
    
    function resetForms() {
        document.forms['myFormName'].reset();
    }
    

    This uses the form name attribute, and if you'd prefer using the form id attribute use this instead:

    document.getElementById('myFormId').reset();
    

提交回复
热议问题