首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在查询字符串中删除表单中的空字段?

如何在查询字符串中删除表单中的空字段?
EN

Stack Overflow用户
提问于 2011-05-08 17:39:26
回答 7查看 10.4K关注 0票数 26

我有一个简单的表单,有四个输入。当我提交表单时,我想使用GET http方法。

例如:

代码语言:javascript
复制
aaa : foo
bbb : ____
ccc : bar
ddd : ____

我想让这个查询字符串:

代码语言:javascript
复制
/?aaa=foo&ccc=bar

问题是我有这个查询字符串:

代码语言:javascript
复制
/?aaa=foo&bbb=&ccc=bar&ddd=

如何从查询字符串中的表单中删除空字段?

谢谢。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2011-05-16 19:29:02

您可以使用jQuery的submit函数来验证/更改表单:

代码语言:javascript
复制
<form method="get" id="form1">
    <input type="text" name="aaa" id="aaa" /> 
    <input type="text" name="bbb" id="bbb" /> 
    <input type="submit" value="Go" />
</form>
<script type="text/javascript">
    $(document).ready(function() {
        $("#form1").submit(function() {
            if($("#bbb").val()=="") {
                    $("#bbb").remove();
            }
        });
     });
</script>
票数 13
EN

Stack Overflow用户

发布于 2015-05-03 09:35:32

当前的解决方案依赖于运行Javascript/jQuery的客户端--这是不能保证的。另一种选择是使用重写规则。在Apache服务器上,为了从表单提交中剥离空的$_GET值,我使用了以下代码;

代码语言:javascript
复制
RewriteCond %{REQUEST_URI} \/formpage\/?
RewriteCond %{QUERY_STRING} ^(.*)(&|\?)[a-z-_]+=(?=&|$)(.*)$ [NC]
RewriteRule .* \/formpage?%1%2 [R,L,NE]

..。这将使这个..;

http://www.yoursite.com/formpage?search=staff&year=&firstname=&surname=smith&area=

..。进入这一领域;

http://www.yoursite.com/formpage?search=staff&surname=smith

简单解释一下:

  1. RewriteCond %{REQUEST_URI} \/formpage\/?是一种将正则表达式的作用域限制在站点地址的特定子页(例如,http://www.yoursite.com/formpage)的方法。这并不是必须的,但您可能希望仅将该表达式应用于表单所在的页面。这是实现this.
  2. RewriteCond %{QUERY_STRING} ^(.*)(&|\?)[a-z-_]+=(?=&|$)(.*)$ [NC]是第二个条件的一种方式,然后匹配查询字符串(即,出现在(或包括)问号中的任何内容,如?firstname=sam$surname=smith)。让我们来分析一下;

代码语言:javascript
复制
- `^(.*)` - The `^` symbol signifies that this is the START of the query string, and the period `.` is saying ANY character.  The asterisk `*` is a modifier against the period wildcard, saying that any number of them should be included in the scope.
- `(&|\?)[a-z-_]+=(?=&|$)` - This is the most interesting bit (if you like this kind of thing ...).  This finds your empty query string.  Between the first parenthesis, we're stating that the string begins with an ampersand OR a literal question mark (backslash makes the following character literal, in this case - otherwise the question mark has a different meaning).  Between the square brackets, we're saying match any character from a-z, OR hyphen `-` OR underscore `_`, and immediately after that the plus symbol `+` is stating that we should match any of those preceding characters 1 or more times.  The equals sign `=` is just what it looks like (the equals between your query string name and its value). Finally, between the next parenthesis, we have a lookahead clause `?=`that states that the next character MUST BE an ampersand OR the very end of the line, as indicated by the dollar sign `$`.  The intention here is that your query string will only be considered a match if it's followed by the start of another query string or the end of the line _without_ including a value.
- The final bits `(.*)$ [NC]` are just matching against any and all content that follows an empty query string, and the `[NC]` clause means that this is cASe iNsENsiTIve.

  1. RewriteRule .* \/formpage?%1%2 [R,L,NE]是我们告诉mod_rewrite如何处理匹配内容的地方。基本上,除了匹配的空字符串之外,在整个查询字符串之后重写URL到您的页面名称。因为这将遍历您的URL,直到它停止查找匹配,所以您是否有一个空值或50都无关紧要。它应该将它们全部找出来,只留下与值一起提交的参数。重写规则上的[NE]子句意味着字符不会被URL编码-如果您需要特殊字符,这可能对您有用,也可能对您没有帮助(但您在处理$_GET数据时显然需要对其进行清理,无论如何都应该这样做)。

再说一次,这显然是一个使用mod_rewrite的Apache解决方案。如果您在其他操作系统(如Windows服务器)上运行,则需要相应地进行调整。

希望这对某些人有用。

票数 7
EN

Stack Overflow用户

发布于 2013-05-30 23:33:44

我喜欢RaphDG给出的想法

但我稍微修改了一下代码。我只使用disabled属性,而不是删除字段。以下是更改后的代码:

代码语言:javascript
复制
<form method="get" id="form1">
    <input type="text" name="aaa" id="aaa" /> 
    <input type="text" name="bbb" id="bbb" /> 
    <input type="submit" value="Go" />
</form>
<script type="text/javascript">
    $(document).ready(function() {
        $("#form1").submit(function() {
            if($("#bbb").val()=="") {
                    $("#bbb").prop('disabled', true);
            }
        });
     });
</script>

再次感谢idea RaphDG (y)

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5926673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档