作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些单选按钮和复选框,比如
<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/
我是一名优秀的程序员,十分优秀!