2010-01-22 87 views
0

我可以在$('#formid').submit(function() { });調用中使用jQuery攔截/詢問表單提交值嗎?我無法檢查頁面中的值,因爲它是我需要檢查的特定提交按鈕。我找到了這個http://geekswithblogs.net/renso/archive/2009/09/09/intercept-a-form-submit-with-jquery-and-prevent-or-allow.aspx,它在FF中工作,但不在IE8中,所以這不起作用,不幸的是。我也不希望將提交代碼附加到按鈕上,因爲我希望能夠將一些通用代碼插入到一組表單中。jQuery表單提交參數檢查

乾杯

MH

回答

2

最新的jQuery 1.4支持「活」,現在事件提交 - 這意味着你不必單獨處理程序連接到您的所有形式。一個很好的例子,涵蓋你問什麼是保羅愛爾蘭這裏給出:

http://jquery14.com/day-05/jquery-1-4-hawtness-1-with-paul-irish

這是我自己的看法:

jQuery(document).ready(function() { 

    var pageForms = jQuery('form'); 

    pageForms.find('input[type="submit"]').live('click', function(event) { 
    var submitButton = this; 
    var parentForm = jQuery(jQuery(this).parents('form')[0]); 
    parentForm.data('submit-button',submitButton); 
    }); 

    pageForms.live('submit', function(event) { 

    // Prevent form-submission. You can do this conditionally later, of course 
    event.preventDefault(); 

    // The form that was submitted 
    var theForm = jQuery(this); 

    // Detect which submit button was pushed 
    var submitButton = theForm.data('submit-button'); 
    console.log('submitButton = ',submitButton.value); 

    }); 

}); 

HTML:

<form> 
    <input type="submit" value="submit form 1" /> 
</form> 

<form> 
    <input type="submit" value="submit form 2" /> 
    <input type="submit" value="submit form 3" /> 
</form> 

http://jsbin.com/equho3/6/edit

編輯 - 對不起,我在這裏發佈了一個例子匹配你的鏈接中的一個!我現在提供了一個跨瀏覽器的解決方案。

+0

有趣的是,感謝您的信息。 –

+0

謝謝 - 我已經通過了一個很好的看看,但我不明白它涵蓋詢問提交參數 - 我錯過了什麼? –

+0

我使用更好的示例更新了我的答案,以顯示如何檢索任何提交表單的提交按鈕值。 – shuckster