如何从没有字段的GET表单中删除尾随问号?

Lav*_*dor 15 html forms get

例:

<form>
    <input type='submit'>
</form>
Run Code Online (Sandbox Code Playgroud)

提交结果时:

http://example.com/?
Run Code Online (Sandbox Code Playgroud)

如何制作:

http://example.com/
Run Code Online (Sandbox Code Playgroud)

[这是一个非常简单的问题示例,实际表单有很多字段,但有些字段有时会被禁用.当所有人都被禁用时,尾随?出现]

And*_*ipe 7

在我的情况下,我使用window.location,不确定它是最好的选择,但它是我可以让它工作的唯一一个:

$('#myform').submit(function()
{
    ... if all parameters are empty

    window.location = this.action;
    return false;
});
Run Code Online (Sandbox Code Playgroud)

我真正的用途是将 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;
});
Run Code Online (Sandbox Code Playgroud)