Shorten URL by removing empty GET variables and simplifying variable names

后端 未结 4 927
温柔的废话
温柔的废话 2021-01-25 18:19

I am working on a website where an URL is composed after submitting a GET form. The form values are passed as an array of variables of which at least one must b

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-25 19:00

    $('#your-form').submit(function () {
        var data = $(this).serializeArray().filter(function (item) {
            return !!item.value;
        });
        var param = jQuery.param(data);
        window.location = this.action + (param ? "?" + param : "");
        return false;
    });
    

提交回复
热议问题