我試圖從我不控制的網頁攔截表單提交。JavaScript:攔截表單提交併確定提交的表單提交
我目前的實現是...
// During onLoad, loop through all forms and for each form object
var prevonsubmit = formobj.onsubmit;
if (prevonsubmit) {
formobj.onsubmit = function f() {
if(prevonsubmit()) {
interceptform();
return true;
}
return false;
};
} else {
formobj.onsubmit = function ff() {
interceptform();
return true;
};
}
這裏的問題是,裏面interceptform(),我無法確定哪些形式實際上是由本次提交。有沒有一種方法,我真的得到了試圖提交的表單對象?請記住,我看到的一些表單沒有指定名稱或ID,並且存在多個表單(在同一網頁中)具有相同的操作。
編輯: 目的是捕獲屬於表單的輸入標籤中的內容。 一個由例如我在一個形式看:
<form action="https://duckduckgo.com/html/" method="GET">
<input type="text" name="q"/>
</form>
<form action="https://duckduckgo.com/html/" method="GET">
<input type="text" name="l"/>
</form>
<form action="https://duckduckgo.com/html/" method="GET">
<input type="text" name="l"/>
<input type="text" name="q"/>
</form>
EDIT2:
基於@ruakh答案,解決我最終使用:
var prevonsubmit = formobj.onsubmit;
if (prevonsubmit)
formobj.onsubmit = createOnSubmitFunctionWithOld(prevonsubmit, formobj);
else
formobj.onsubmit = createOnSubmitFunction(formobj);
// Definition of the functions:
function createOnSubmitFunctionWithOld(prevonsubmit,formObj) {
return function() {
if (prevonsubmit()) {
interceptform(formObj);
return true;
}
return false;
};
}
function createOnSubmitFunction(formObj) {
return function() {
interceptform(formObj);
return true;
};
}
確定提交表單的目的是什麼?你可以發佈一個頁面上的表單和例子,以便我們看到你在處理什麼? – prodigitalson 2012-03-26 00:55:05
@prodigitalson,目的是從提交中獲取文本。我添加了這個問題的目的和形式的一個例子。 – mani 2012-03-26 01:05:04