Html 如何从没有字段的GET表单中删除尾随问号?
例如:Html 如何从没有字段的GET表单中删除尾随问号?,html,forms,get,Html,Forms,Get,例如: <form> <input type='submit'> </form> <button type="button" value="Play as guest!" title="Play as guest!" onclick="location.href='/play'">Play as guest!</button> 如何制作: http://example.com/ ? [这是一个非常简单的问题示例,实际表单有
<form>
<input type='submit'>
</form>
<button type="button" value="Play as guest!" title="Play as guest!" onclick="location.href='/play'">Play as guest!</button>
如何制作:
http://example.com/
?
[这是一个非常简单的问题示例,实际表单有许多字段,但有些字段有时会被禁用。当所有字段都被禁用时,会出现尾随字符]我正在寻找类似的答案。我最后做的是创建一个按钮,当点击时,它会重定向到某个页面 例如:
<form>
<input type='submit'>
</form>
<button type="button" value="Play as guest!" title="Play as guest!" onclick="location.href='/play'">Play as guest!</button>
以来宾身份玩!
这不是你问题的“答案”,但可能是一个很好的解决办法。我希望这有帮助。如果不使用Javascript,我不确定是否有Javascript。缓解此问题的一种方法可能是创建一个隐藏的输入,其中只包含一些垃圾值,您可以在另一端忽略这些值,如下所示:
<input type="hidden" name="foo" value="bar" />
这样,您将永远不会有一个空的GET请求。在我的情况下,我使用的是window.location,不确定它是否是最好的选择,但它是我唯一可以使其工作的方法:
$('#myform').submit(function()
{
... if all parameters are empty
window.location = this.action;
return false;
});
我真正的用途是将GET参数转换为真实的url路径,下面是完整的代码:
$('#myform').submit(function()
{
var form = $(this),
paths = [];
// get paths
form.find('select').each(function()
{
var self = $(this),
value = self.val();
if (value)
paths[paths.length] = value;
// always disable to prevent edge cases
self.prop('disabled', true);
});
if (paths.length)
this.action += paths.join('/')+'/';
window.location = this.action;
return false;
});
这是一个老帖子,但是嘿。。给你 如果您使用的是类似PHP的东西,那么可以将表单提交到“代理”页面,该页面将标题重定向到特定位置+查询 例如: HTML:
PHP(proxy.PHP)
我假设您正在尝试这样做,另一个选项是在提交之前用javascript检查FormData
var myNeatForm = document.getElementById("id_of_form");
var formData = new FormData(myNeatForm); // Very nice browser support: https://developer.mozilla.org/en-US/docs/Web/API/FormData
console.log(Array.from(formData.entries())); // Should show you an array of the data the form would be submitting.
// Put the following inside an event listener for your form's submit button.
if (Array.from(formData.entries()).length > 0) {
dealTypesForm.submit(); // We've got parameters - submit them!
} else {
window.location = myNeatForm.action; // No parameters here - just go to the page normally.
}
我知道这是一个非常古老的问题,但我今天遇到了同样的问题。我会从一个不同的角度来处理这个问题,我的想法是,在今天这个时代,你可能应该使用POST,而不是使用表单,因为在查询字符串中传递值对安全性和GDPR并没有好处。我们已经解决了很多问题,各种跟踪脚本一直在使用querystring(参数中包含PII),破坏了它们所拥有的任何服务条款
通过发布,您将始终获得“干净的url”,并且不需要对表单提交脚本进行任何修改。但是,如果表单输入需要GET,您可能需要更改接收表单输入的内容。挂起的问号与附录一样有用。。。有时也很麻烦。。。喜欢这些变通方法!:-)我同意。这很烦人。我在Google Chrome上看到了,但在Internet Explorer Edge上看不到。原始铬缺陷:
var myNeatForm = document.getElementById("id_of_form");
var formData = new FormData(myNeatForm); // Very nice browser support: https://developer.mozilla.org/en-US/docs/Web/API/FormData
console.log(Array.from(formData.entries())); // Should show you an array of the data the form would be submitting.
// Put the following inside an event listener for your form's submit button.
if (Array.from(formData.entries()).length > 0) {
dealTypesForm.submit(); // We've got parameters - submit them!
} else {
window.location = myNeatForm.action; // No parameters here - just go to the page normally.
}