简体   繁体   English

发送带有jQuery的表格,网址中不带问号

[英]Send form with jquery without question mark in url

I'm doing a userscript for a site and want to add some button on it, that opens categories from it. 我正在为网站制作用户脚本,并希望在网站上添加一些按钮,以从中打开类别。 They have search box with drop down menu where you should select the category and to click search buttons and then you're navigated to the category. 它们具有带下拉菜单的搜索框,您应该在其中选择类别并单击搜索按钮,然后导航到该类别。

So I want to add 3, 4 buttons with this categories and every button contains the link to the every category. 因此,我想添加3、4个具有此类别的按钮,每个按钮都包含指向每个类别的链接。 But when you click any button in url is add "?", which is ok in first page, but when you have pagination and click second page this ? 但是,当您单击url中的任何按钮时,会添加“?”,这在第一页中是可以的,但是当您进行分页并单击第二页时,此? break the url and don't navigate to second page. 断开网址,不要导航到第二页。

example: 例:

http://example.com/torrents/type:pc-iso/

instead of 代替

 http://example.com/torrents/type:pc-iso/?

So is there a better way when click a button to navigate me into the right url without question mark? 那么,单击按钮导航到没有问号的正确网址时,有没有更好的方法?

Here is my code: 这是我的代码:

$(this).append(
        "<br />" + "<br />" + "<br />" +
        "<form id=\"beBossButtons\">" +
        "<table><tr>" +
        "<td><button id=\"all-btn\"  class=\"btn btn-sm btn-green4\"><i class=\"fa fa-search\"></i> All</button></td>" +
        "<td><button id=\"pc-iso-btn\"  class=\"btn btn-sm btn-green4\"><i class=\"fa fa-search\"></i> Software PC-ISO</button></td>" +
        "<td><button id=\"documentaries-btn\" class=\"btn btn-sm btn-green4\"><i class=\"fa fa-search\"></i> Documentaries</button></td>" +
        "</tr></form>");
});

$('#all-btn').click(function () {
    var form = document.getElementById("beBossButtons");
    form.action = "/torrents/";
    form.submit();
});

$('#pc-iso-btn').click(function () {
    var form = document.getElementById("beBossButtons");
    form.action = "/torrents/type:pc-iso/";
    form.submit();
});

$('#documentaries-btn').click(function () {
    var form = document.getElementById("beBossButtons");
    form.action = "/torrents/type:documentaries/";
    form.submit();
});

Well, I'm ready and make this post to show how I did it. 好了,我已经准备好发布这篇文章来展示我是如何做到的。

First added method post to form 首先添加方法后形成表格

$('.col-md-6 .form-group.mb0').each(function () {
    $(this).append(
        "<br />" + "<br />" + "<br />" +
        "<form id=\"beBossButtons\" method=\"post\">" +
        "<table><tr>" +
        "<td><button id=\"all-btn\"  class=\"btn btn-sm btn-green4\"><i class=\"fa fa-search\"></i> All</button></td>" +
        "<td><button id=\"pc-iso-btn\"  class=\"btn btn-sm btn-green4\"><i class=\"fa fa-search\"></i> Software PC-ISO</button></td>" +
        "<td><button id=\"documentaries-btn\" class=\"btn btn-sm btn-green4\"><i class=\"fa fa-search\"></i> Documentaries</button></td>" +
        "</tr></form>");
});

I saw how they send search form and their attributes and copied them (type: category). 我看到了他们如何发送搜索表单及其属性并复制了它们(类型:类别)。 Added this attribute to my form. 将此属性添加到我的表单。 Changed action with "search" instead of url, so in this way I'm using their search form. 使用“搜索”而不是网址更改了操作,因此我以这种方式使用了他们的搜索表单。

Old jQuery: 旧的jQuery:

$('#pc-iso-btn').click(function () {
    var form = document.getElementById("beBossButtons");
    form.action = "/torrents/type:pc-iso/";
    form.submit();
});

New and working one: 新的工作方式之一:

$('#pc-iso-btn').click(function () {
    var form = document.getElementById("beBossButtons");
    var input = $("<input>")
        .attr("type", "hidden")
        .attr("name", "type").val("pc-iso");
    $('#beBossButtons').append($(input));
    form.action = "search";
    form.submit();
});

And That's all. 就这样。 Thanks for you help. 感谢您的帮助。

You would have better luck with a list of links tied to an ajax request 与Ajax请求相关的链接列表会给您带来更好的运气

<a href="http://example.com/torrents/type:pc-iso/" class="button">Category 1</a>
<a href="http://example.com/torrents/type:mac-os/" class="button">Category 2</a>

<script>
$("a.button").click(function(){
    $.ajax({
        url: $(this).attr('href'),
        type: "GET",
        success: function(data){

            // Load your categories data on success

        }
    });
});
</script>

Clicking on either category loads up the required content. 单击任一类别将加载所需的内容。 Using a form to post your data to which seems like a GET request will be overkill 使用表单将您的数据发布到看起来像是GET请求的表单上会显得过时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM