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