当您使用 method='get' 提交 HTML 表单时,表单中的值将被格式化为 GET 请求,例如
www.site.com/script.php?var1=value&var2=value&...
据我所知,如果表单上的任何项目未指定,它们仍会放入字符串中。如果上面示例中的 var1 未指定,您会看到...
www.site.com/script.php?var1=&var2=value&...
有没有办法让表单在 GET 请求中不包含任何未指定的值(最好没有 javascript)?
最佳答案
没有必要这样做。您可以使用 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>
但请记住,这不是一个好的做法!
关于html - 如何从 HTML GET 请求中排除未指定的表单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10274432/