当您使用method ='get'提交HTML表单时,表单中的值将被格式化为GET请求
www.site.com/script.php?var1=value&var2=value&...
Run Code Online (Sandbox Code Playgroud)
据我所知,如果表单中的任何项目未指定,它们仍会被放入字符串中.如果未指定上例中的var1,您会看到......
www.site.com/script.php?var1=&var2=value&...
Run Code Online (Sandbox Code Playgroud)
有没有办法使表单不包含GET请求中的任何未指定的值(最好没有javascript)?
小智 2
没有必要这样做。您可以使用 PHP 轻松处理发送的变量。但如果您真的热衷于这样做,您可以使用 jQuery。
无论如何你可以做这样的事情:
<form action="index.html" method="get">
<input name="name">
<input name="name2">
<input name="name3">
<input type="submit">
</form>
<script type="text/javascript">
$("form").submit(function() {
$("form input").each(function(index, element) {
if(($(this).val()=="")){ $(this).attr("disabled","disabled"); }
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
但请记住,这不是一个好的做法!