【问题标题】:Modify code in OnClick with Javascript使用 Javascript 修改 OnClick 中的代码
【发布时间】:2011-03-30 04:10:07
【问题描述】:

我调用了一个之前已经创建的 onclick。 该按钮已经有一个 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; }
  };
}

这是之前代码中的alert

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;
}

【问题讨论】:

    标签: javascript javascript-events


    【解决方案1】:

    试试这个:让动作值成为一个变量。

    var oldURI = "http://mysite.com:8080..."
    var href = "oldURI" + "&newParam=true"
    
    document.forms.Maker.action='href'
    

    【讨论】:

    • 这就是我想要做的,将 Action 放入一个变量中。但是 onclick 对象不允许我将其转换为字符串。
    【解决方案2】:

    如果此 Html 是您自己的代码,请这样做:

    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;
    }
    

    但是记得关闭浏览器的跨域数据源访问限制!!!

    例如在 ie 中: Internet 选项-> 安全-> 自定义设置(Internet 区域)->

    启用跨域访问数据源

    【讨论】:

    • 这不是我的代码,它被渲染了,参数中的值可能会改变。所以我需要在最后添加它,不管其他参数有什么。另外,我不控制浏览器,所以不能使用最后一个。
    猜你喜欢
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多