How can I remove empty fields from my form in the querystring?

后端 未结 6 1574
滥情空心
滥情空心 2020-12-16 11:17

I have a simple form with four inputs. When I submit my form, I want to use the GET http method.

For the example :

aaa : foo
bbb : ____
ccc : bar
ffffd         


        
6条回答
  •  青春惊慌失措
    2020-12-16 11:28

    With jQuery:

    $('form[method="get"]').submit(function(){
        $(this).find(':input').each(function() {
            var inp = $(this);
            if (!inp.val()) {
                inp.remove();
            }
        });
    });
    

    I use this to remove all empty parameters from all GET forms on my site. The :input pseudo selector covers all inputs, textareas, selects and buttons.

提交回复
热议问题