2009-12-29 124 views

回答

2

您可以使用jQuery AJAX .post function函數。一個例子(未經測試,但應工作):

<script> 
function postit(obj) { 
    var data = $(obj).serialize(); 
    $.post($(obj).attr("action"), data, function() { 
    //Put callback functionality here, to be run when the form is submitted. 
    }); 
} 
</script> 
<form action="posthandler.php" onsubmit="postit(this); return false;"> 
<input type="text" name="field"> 
<input type="submit"> 
</form> 

此外,閱讀有關serialize

(當然,你需要在你的代碼jQuery庫使用此代碼之前)。

1

我不確定是否正確理解了這個問題。

如果您不想使用submit(),則可以使用Ajax通過jQuery.post()執行相同的操作。主要的區別是你必須自己構造輸入字段的關鍵值數據,而不是瀏覽器自動執行此操作,並且不會刷新頁面。

+0

u能請說明jQuery的崗位? – RKh 2009-12-29 12:40:01

+0

我給出的鏈接充滿了一些例子,這讓我發表一個有點毫無意義的例子。 – Yacoby 2009-12-29 12:42:53

2

只需創建一個由你想要的任何事件觸發的功能,例如:(發現另一個問題的代碼)

function example() { 
// get all the inputs into an array. 
    var $inputs = $('#myForm :input'); 

    // not sure if you wanted this, but I thought I'd add it. 
    // get an associative array of just the values. 
    var values = {}; 
    $inputs.each(function() { 
     values[this.name] = $(this).val(); 
    }); 
} 

之後,你可以做任何你想要的輸入值。你可能想要考慮使用更高級的處理,但是有很多插件可以提供這種功能。

1

Post函數或Load函數都可以使用。

@PRK你是否試圖在頁面加載或用戶點擊按鈕時發佈表單?

負載(URL,參數,回調)

如:

$("#loadItHere").load("some.php", {somedata: 1}); 
相關問題