2012-10-18 106 views
1

當使用GET方法提交表單時,我正在重新加載頁面的一部分。下面的JavaScript運行,但我不確定如何從窗體中獲取提交的URL?通過jQuery/Ajax獲取GET URL

我會想到var link = jQuery(this).attr('GET');或somethign會返回它,並嘗試URL等......沒有運氣,但。有任何想法嗎?

jQuery('form').submit(function(e){ 
    e.preventDefault(); 
    var link = jQuery(this).attr('GET'); //Get the href attribute 
    alert(link); 
    history.pushState('data', '', link); 
    jQuery('#search_main_section').fadeTo(300,0.3,function(){ 
     load(link + ' #search_main_section', function(){ jQuery("#loader").hide(); 
     jQuery('#search_main_section').fadeTo(100,1, function(){}); 
    }); 
}); 

回答

3
var link = jQuery(this).prop('action'); 

如果你想表單的值,你想用用行動serialize()

實例和負載searlize。

$("#form1").on("submit", function(e) { 

    e.preventDefault(); 

    var form = $(this); 
    var action = form.attr("action") || window.location.href; 
    var formValues = form.serialize(); 
    //example with get  
    //$.get(action, formValues, function(){ alert("done"); }); 
    //example with load 
    var url = action + "?" + formValues; 
    $.load(url + ' #search_main_section', function(){ jQuery("#loader").hide(); 
});​​​​​​​​​​ 
+0

不要忘記返回FALSE;最後! – silly

+2

@silly,這是preventDefault在OP代碼中的作用。 – epascarello

+0

對,對不起! +1! – silly

0
<form action="something.php" method="POST"> 

</form> 


jQuery('form').submit(function(e){ 
    e.preventDefault(); 
    var url = $(this).attr('action'); 
    ....// rest of code