表单获取方法:防止在查询字符串中提交空字段 [英] Form Get Method: prevent empty fields from submittion in query string

查看:23
本文介绍了表单获取方法:防止在查询字符串中提交空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个搜索表单.

I am developing a search form.

搜索表单有两部分,首先使用一些选择、输入和提交按钮进行简单搜索.第二个包含许多选择、复选框、单选、输入和提交按钮.

Search Form has 2 parts, First simple search with some select, input, and submit button. Second contains many select, check box, radio, input and submit button.

我正在使用 GET 方法,因为我想要查询字符串中的所有字段.example.com/cars-parts/cars-for-sale/?country=205&city=19&cars_category=462

I am using GET method as i want all fields in query string like. example.com/cars-parts/cars-for-sale/?country=205&city=19&cars_category=462

但问题是如果用户不选择某些字段(因为他不想应用此条件)很多空字段并使查询字符串过长.

But the issue is if user don't select some fields(because he donot want to apply this criteria) a lot of empty fields and make query string too long.

问题:

我想在提交表单之前删除(禁用或任何需要的)所有空字段,以便查询字符串仅包含具有值的字段.

I want to remove (disable or whatever needed) all empty fields before form submission, so that query string includes only fields having value.

注意:仅建议使用 Form Get 方法.在过去,我使用 POST 方法并且没有在查询字符串中传递值,但现在我想要查询字符串中的值.

NOTE: Only suggest using Form Get Method. In Past i was using POST method and no values were passed in query string, but now i want values in query string.

感谢您的阅读.

推荐答案

您可以在表单提交事件上绑定一个函数并删除/禁用所有带有空值的输入.像这样:

You can bind a function on the form submit event and remove/disable all input with an empty value. Like this :

your_form.submit(function() {
    your_form.find('input').each(function() {
        var input = $(this);
        if (!input.val()) {
            input.remove(); // or input.prop('disabled', true);
        }
    });
});

这篇关于表单获取方法:防止在查询字符串中提交空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆