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

前端 未结 4 1224
一生所求
一生所求 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();
    
    0 讨论(0)
  • 2020-12-31 08:30

    you can simply redirect page to current location with this code:

    $('[data-command="reset"]').click(function () {    
        window.location.href = window.location.href;
    }
    

    <input type="button" value="Clear Results" data-command="reset">
    
    0 讨论(0)
  • 2020-12-31 08:38

    using JQuery, do something like this on the page;

    $(document).ready(function () {
        resetForms();
    });
    
    function resetForms() {
        for (i = 0; i < document.forms.length; i++) {
            document.forms[i].reset();
        }
    }
    

    and then just use your second input, forms will auto refresh when the page loads back up.

    0 讨论(0)
  • 2020-12-31 08:43

    If you want the functionality of both of the snippets you posted, you can simply combine them.

    <input type="reset" value="Reset" onClick="window.location.reload()">
    
    0 讨论(0)
提交回复
热议问题