使用javascript比较两个html按钮的onclick动作

发布于 2024-11-09 10:24:36 字数 1346 浏览 0 评论 0原文

我有这两个 HTML 表单按钮,每个按钮都有一个关联的 onclick 操作。

<input type=button name=sel value="Select all" onclick="alert('Error!');">
<input type=button  name=desel value="Deselect all" onclick="alert('Error!');">

不幸的是,这个动作会不时发生变化。它可以是

onclick="";>

或者

onclick="alert('Error!');"

不幸的是,

onclick="checkAll('stato_nave');"  

我正在尝试编写一些 javascript 代码来验证调用的函数是什么,并在需要时更改它:

var button=document.getElementsByName('sel')[0];
// I don't want to change it when it is empty or calls the 'checkAll' function
if( button.getAttribute("onclick") != "checkAll('stato_nave');" &&
    button.getAttribute("onclick") != ""){
    //modify button
    document.getElementsByName('sel')[0].setAttribute("onclick","set(1)");
    document.getElementsByName('desel')[0].setAttribute("onclick","set(0)");
} //set(1) and set(0) being two irrelevant function

这些都不起作用。 回顾一些步骤,我注意到它

alert( document.getElementsByName('sel')[0].onclick);

没有像我预期的那样输出 onclick 内容,而是输出:

function onclick(event) {
    alert("Error!");
}

所以我猜由于这个原因比较失败,我无法将 functionstring 进行比较
有人猜测如何区分哪个函数与 onclick 属性关联吗?

I have this two HTML Form buttons with an onclick action associated to each one.

<input type=button name=sel value="Select all" onclick="alert('Error!');">
<input type=button  name=desel value="Deselect all" onclick="alert('Error!');">

Unfortunately this action changes from time to time. It can be

onclick="";>

or

onclick="alert('Error!');"

or

onclick="checkAll('stato_nave');"  

I'm trying to write some javascript code that verifies what is the function invoked and change it if needed:

var button=document.getElementsByName('sel')[0];
// I don't want to change it when it is empty or calls the 'checkAll' function
if( button.getAttribute("onclick") != "checkAll('stato_nave');" &&
    button.getAttribute("onclick") != ""){
    //modify button
    document.getElementsByName('sel')[0].setAttribute("onclick","set(1)");
    document.getElementsByName('desel')[0].setAttribute("onclick","set(0)");
} //set(1) and set(0) being two irrelevant function

Unfortunately none of this work.
Going back some steps I noticed that

alert( document.getElementsByName('sel')[0].onclick);

does not output the onclick content, as I expected, but outputs:

function onclick(event) {
    alert("Error!");
}

So i guess that the comparisons fails for this reason, I cannot compare a function with a string.
Does anyone has a guess on how to distinguish which function is associated to the onclick attribute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽清梦 2024-11-16 10:24:37

尝试将 onclick 属性转换为 string。那么你至少可以检查checkAll的索引以及它是否为空。之后,您可以轻松地将这些 input 元素绑定到新的 onclick 函数。


var sel = document.getElementsByName('sel')[0];
var desel = document.getElementsByName('desel')[0];

var onclick = sel.getAttribute("onclick").toString();

if (onclick.indexOf("checkAll") == -1 && onclick != "") {
    sel.onclick = function() { set(1) };
    desel.onclick = function() { set(0) };
}

function set(number)
{
    alert("worked! : " + number);
}

工作示例: http://jsfiddle.net/fAJ6v/1/


工作示例,当有 <代码>checkAll方法:http://jsfiddle.net/fAJ6v/3/

Try casting the onclick attribute to a string. Then you can at least check the index of checkAll and whether it is empty. After that you can bind those input elements to the new onclick functions easily.


var sel = document.getElementsByName('sel')[0];
var desel = document.getElementsByName('desel')[0];

var onclick = sel.getAttribute("onclick").toString();

if (onclick.indexOf("checkAll") == -1 && onclick != "") {
    sel.onclick = function() { set(1) };
    desel.onclick = function() { set(0) };
}

function set(number)
{
    alert("worked! : " + number);
}

working example: http://jsfiddle.net/fAJ6v/1/


working example when there is a checkAll method: http://jsfiddle.net/fAJ6v/3/

谁许谁一生繁华 2024-11-16 10:24:36

这有效

http://jsfiddle.net/mplungjan/HzvEh/

var button=document.getElementsByName('desel')[0];
// I don't want to change it when it is empty or calls the 'checkAll' function
var click = button.getAttribute("onclick");
if (click.indexOf('error') ) {

    document.getElementsByName('sel')[0].onclick=function() {setIt(1)};
    document.getElementsByName('desel')[0].onclick=function() {setIt(0)};

}

function setIt(num) { alert(num)}

但是为什么不将 onclick 移动到脚本

window.onload=function() {
  var button1 = document.getElementsByName('sel')[0];
  var button2 = document.getElementsByName('desel')[0];
  if (somereason && someotherreason) {
    button1.onclick=function() {
      sel(1);
    }
    button2.onclick=function() {
      sel(0);
    }
  }
  else if (somereason) {
    button1.onclick=function() {
      alert("Error");
    }
  }
  else if (someotherreason) {
    button1.onclick=function() {
      checkAll('stato_nave')
    }
  }
}

This works

http://jsfiddle.net/mplungjan/HzvEh/

var button=document.getElementsByName('desel')[0];
// I don't want to change it when it is empty or calls the 'checkAll' function
var click = button.getAttribute("onclick");
if (click.indexOf('error') ) {

    document.getElementsByName('sel')[0].onclick=function() {setIt(1)};
    document.getElementsByName('desel')[0].onclick=function() {setIt(0)};

}

function setIt(num) { alert(num)}

But why not move the onclick to a script

window.onload=function() {
  var button1 = document.getElementsByName('sel')[0];
  var button2 = document.getElementsByName('desel')[0];
  if (somereason && someotherreason) {
    button1.onclick=function() {
      sel(1);
    }
    button2.onclick=function() {
      sel(0);
    }
  }
  else if (somereason) {
    button1.onclick=function() {
      alert("Error");
    }
  }
  else if (someotherreason) {
    button1.onclick=function() {
      checkAll('stato_nave')
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文