Yii2 从 url 字符串中删除空字段

Yii2 remove empty fields from url string

我需要你的帮助。我的 Yii2 应用程序中有一个活动表单,当我提交表单时,它会显示表单每个字段的值(无论是否为空)到 GET 字符串,所以它看起来像 domain.com/index?ItemSearch%5Brooms_arr%5D=&ItemSearch%5Bprice_type%5D=&ItemSearch%5Bprice_type%5D=0&ItemSearch%5Barea_from%5D=&ItemSearch%5Barea_to%5D=&...

我需要更清晰的查询字符串,它只包含非空参数? 例如domain.com/index?rooms_arr=12&price_type=normal

请建议我最好的方法是什么?

这不是yii2的问题。它是原生的 html 形式,工作方式如下。如果你真的想从查询字符串中排除所有未填充的输入,你可以通过 jQuery 过滤所有这些参数并将它们设置为禁用状态,这里是代码

$('form').submit(function(e){
    var emptyinputs = $(this).find('input').filter(function(){
        return !$.trim(this.value).length;  // get all empty fields
    }).prop('disabled',true);    
});