jQuery onclick div检查一个复选框 [英] jQuery onclick div checks a checkbox

查看:52
本文介绍了jQuery onclick div检查一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML代码:

I have the following HTML code:

<div class="member">
    <input type="checkbox" value="64" style="display:none;" name="memberId[]" />
    <div class="memberInfo">John Doe</div>
</div>

我正在尝试获取一个jQuery脚本,当访问者按下会员 div它会改变背景颜色,并检查 memberId 复选框。

I'm trying to get a jQuery script that when a visitor presses the member div it will change the background color, and check the memberId checkbox.

这样的jQuery脚本应该怎么样?

How should such a jQuery script look like?

谢谢,

推荐答案

您应该通过 切换 来实现此目的你设置两个处理程序:一个是第一次触发处理程序,然后是下一个处理程序,必要时重复。

You should probably do this with toggle which allows you to set two handlers: one for the first time a handler is triggered and then one for the next time, repeating as necessary.

$('.member').toggle(function() { // first time
    $(this).css('background-color', '#ff0000')
           .find('input:checkbox').attr('checked', true);
}, function() { // second time
    $(this).css('background-color', '#ffffff')
           .find('input:checkbox').attr('checked', false);
});

这篇关于jQuery onclick div检查一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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