2011-03-29 78 views
0

我打電話給之前已經創建的onclick。 該按鈕已經有一個onclick,但我需要在提交之前添加一個參數。使用Javascript修改OnClick中的代碼

這裏的按鈕上的查看源代碼

<td valign='top' align='right' > 
    <button name="Complete" title="Complete" 
    onClick="document.forms.Maker.action='http://mysite.com:8080/internal/Step.jsp?theId=19032&target=step20&type=Full'; 
    document.forms.Maker.submit();return false;">Complete</button> 
</td> 

這是一個修改我發到用戶按下按鈕後,添加一個確認,我還添加了這樣它顯示什麼的onclick目前有:

function addEventConfirmation(element, type){ 
    var old = element['on' + type] || function() {}; 
    element['on' + type] = function() { 

    if (confirm("Are you sure?")){ 
    old.call(this); 
    alert(old); 
    } else { return false; } 
    }; 
} 

這是警報從代碼之前:

function onclick() 
{ 
document.forms.Maker.action='http://mysite.com:8080/internal/Step.jsp?theId=19032& 
target=step20&type=Full';document.forms.Maker.submit();return false; 
} 

結果應顯示是這樣的:

function onclick() 
{ 
document.forms.Maker.action='http://mysite.com:8080/internal/Step.jsp?theId=19032& 
target=step20&type=Full&newParam=true';document.forms.Maker.submit();return false; 
} 

回答

0

試試這個:使動作值的變量。

var oldURI = "http://mysite.com:8080..." 
var href = "oldURI" + "&newParam=true" 

document.forms.Maker.action='href' 
+0

訪問數據源這就是我想要做的,得到把Action變成一個變量。但是onclick對象不會讓我將它轉換爲String。 – elcool 2011-03-30 16:24:02

0

如果這個網站是自己的代碼,這樣做就像這樣:

document.getElementById('Complete').onclick = function() 
{ 
    document.forms.Maker.action= 
      'http://mysite.com:8080/internal/Step.jsp?theId=19032& 
      target=step20&type=Full&newParam=true'; 
    document.forms.Maker.submit(); 
    return false; 
} 

否則,如果你正在試圖改變從其他網站此事件處理程序做到這一點: 放網站在名爲「myframe」的iframe中,代碼如下:

document.getElementById('Complete').onclick = function() 
{ 
    document.forms.Maker.action= 
      'http://mysite.com:8080/internal/Step.jsp?theId=19032& 
      target=step20&type=Full&newParam=true'; 
    document.forms.Maker.submit(); 
    return false; 
} 

但記住關閉瀏覽器的跨域數據源訪問限制!

例如即: Internet選項 - >安全 - >自定義設置(Internet區域) - >

啓用跨域

+0

這不是我的代碼,它被渲染,參數中的值可能會改變。所以我需要在最後添加它,而不管其他參數是什麼。另外,我不控制瀏覽器,所以不能使用最後一個。 – elcool 2011-03-30 16:22:17