【问题标题】:select function in onchange() event depending on function being run [closed]根据正在运行的函数在 onchange() 事件中选择函数 [关闭]
【发布时间】:2013-12-24 09:35:42
【问题描述】:

我有一个 Onchange() 事件,当对选择进行更改时会触发该事件。我想要做的是在 onchange() 事件中有两个函数。但我希望这取决于选择的功能。所以我目前在我的 onchange() 中有:

<SELECT NAME="clothing" onChange="updateforclothingnew(); updateforclothing()">

我有两个 javascript 函数:updateclass 和 updateclassnew

我想要这样,如果 updateclass 运行,那么 updateforclothing() 就会被触发 如果 updateclassnew 运行,则触发 updateforclothingnew()。

如何将它放入我的 onchange() 事件中?

提前致谢!

【问题讨论】:

  • 如果你有相互依赖的函数,为什么不以级联方式调用它们呢?如果运行了 updateclassnew() 那么为什么不从中调用 updateforclothingnew() 呢?

标签: javascript html function select onchange


【解决方案1】:

你必须打破内联 JS,这样它更容易阅读和处理。

试试这个:

1 给选择一个 ID。

<select name="clothing" id="clothing">

2 在脚本中分配 onchange 函数

var x = document.getElementById("clothing");
x.onchange = function() {
    if (x.value == "updateclass") { //or whatever condition you want to run
        updateforclothing();
    } else if (x.value == "updateclassnew") {
        updateforclothingnew();
    }
}

这将根据所选的选项值运行函数(我假设您正在尝试这样做?)

【讨论】:

  • 类似的东西是的,所以我只需要分配任何名称,因为所依赖的值是函数,所以 updateclass() 和 updateclassnew()。我猜上面会起作用吗?我可以试试。
猜你喜欢
  • 1970-01-01
  • 2013-12-27
  • 2021-03-04
  • 1970-01-01
  • 2020-03-27
  • 1970-01-01
  • 2017-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多