2015-06-20 8 views
1

我做了userscript一個網站,並希望增加一些關於它的按鈕,從它打開類別。他們有搜索框和下拉菜單,您應該選擇類別並單擊搜索按鈕,然後導航到該類別。發送表格與jQuery沒有問號的URL

所以,我要加3,4按鍵與此類別和每一個按鈕包含鏈接到每個類別。但是當你點擊url中的任何按鈕時,都會添加「?」,這在第一頁是可以的,但是當你有分頁並且點擊第二頁時呢?打破網址,不要導航到第二頁。

比如:

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

代替

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

那麼,有沒有更好的辦法,當點擊一個按鈕來瀏覽我進不問號正確的網址是什麼?

這裏是我的代碼:

$(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(); 
}); 
+1

你嘗試形式POST方法?請參閱http://www.w3schools.com/tags/att_form_method.asp獲取更多關於 – Kira

+0

表單的方法屬性的信息是的,我嘗試過。使用post方法,url是好的,但頁面是空白的。並加載頁面,我必須點擊進入瀏覽器中的url地址(刷新不起作用) – beBoss

+0

你打開一個新的按鈕點擊鏈接?如果是這樣,請使用錨標記而不是表單 – Kira

回答

0

嗯,我已經準備好了,使這個貼子來展示我是如何做的。

首先添加方法後形成

$('.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>"); 
}); 

我看到他們是如何將搜索形式和它們的屬性和複製他們(類型:類)。將此屬性添加到我的表單中。 更改了「搜索」而不是url的動作,所以用這種方式我使用了他們的搜索表單。

舊的jQuery:

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

新的和工作之一:

$('#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(); 
}); 

,這一切。感謝您的幫助。

0

您將有更好的運氣與拴在任何一類負荷高達所要求的內容的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> 

點擊的鏈接列表。使用表來發表您的數據,這似乎是一個GET請求將是矯枉過正

+0

我會稍後再嘗試,謝謝你的信息:) – beBoss