2011-03-29 75 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