单击任何单选按钮或复选框时调用相同的功能 [英] calling the same function when any radio button or checkbox are clicked

查看:69
本文介绍了单击任何单选按钮或复选框时调用相同的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some radio button and checkboxs like

<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.

I would like to call the calcPrice anytime when radio button or checkbox clicked.

我正在使用jquery,并且知道单击某些按钮时如何调用.

I am using jquery and I know how to call when certain button is clicked.

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

但是,当我单击任意"按钮时,我想调用相同的功能.

But I would like to call the same function when 'any' button are clicked.

我怎么做?

推荐答案

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

Since you may have other input elements in the page, just use a class name to identify them. In my example, I used the class name calc so you can use it to define an on change event.

<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:

For your JavaScript:

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

这篇关于单击任何单选按钮或复选框时调用相同的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆