gpt4 book ai didi

javascript - 单击任何单选按钮或复选框时调用相同的函数

转载 作者:行者123 更新时间:2023-11-30 12:47:39 25 4
gpt4 key购买 nike

我有一些单选按钮和复选框,比如

<script type="text/javascript">

function calcPrice(){
console.log(checked!!);
}
</script>

<input type="radio" name="android" value="1">smart
<input type="radio" name="android" value="2">tablet
<input type="radio" name="android" value="3">both

<input type=checkbox name="func" value="0">push
<input type=checkbox name="func" value="0">distribute
<input type=checkbox name="func" value="0">internationalization

我想在单击单选按钮或复选框时随时调用 calcPrice。

我正在使用 jquery,我知道在单击某个按钮时如何调用。

$("input[name='android']").change(function()

但我想在单击“任何”按钮时调用相同的函数。

我怎样才能做到?

最佳答案

由于页面中可能还有其他输入元素,因此只需使用类名来标识它们。在我的示例中,我使用了类名 calc,因此您可以使用它来定义更改事件。

<script type="text/javascript">

function calcPrice(){
console.log('checked!!'); // note that you missed the quotes here
}
</script>

<input class="calc" type="radio" name="android" value="1">smart
<input class="calc" type="radio" name="android" value="2">tablet
<input class="calc" type="radio" name="android" value="3">both

<input class="calc" type=checkbox name="func" value="0">push
<input class="calc" type=checkbox name="func" value="0">distribute
<input class="calc" type=checkbox name="func" value="0">internationalization

对于您的 JavaScript:

$("input.calc").change(function() {
calcPrice();
});

关于javascript - 单击任何单选按钮或复选框时调用相同的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21978848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com