取消选中复选框时未触发jQuery单击功能

时间:2011-07-21 08:34:46

标签: jquery checkbox click css-selectors

这是我的头脑,我已经搜索了它的生命,但下面是我的代码,非常简单,但我没有在取消选中复选框时触发点击事件?

$('#filterStarDiv #hotelFilterForm #Star0').click( function() {
alert('Checkbox 0 clicked');});

<div id="starsRemoved" style="display:none">No Stars Removed</div>
<div id="filterStarDiv">
<h6>Click on the star ratings below to add or remove hotels in that category</h6>
<form:form id="hotelFilterForm" action="" acceptCharset="UTF-8">
    <input type="checkbox" id="Star0"  name="Star0" value="0 Star" checked="checked" /> 0 Star<br />
    <input type="checkbox" id="Star1"  name="Star1" value="1 Star" checked="checked" /> 1 Star<br />
    <input type="checkbox" id="Star2"  name="Star2" value="2 Star" checked="checked" /> 2 Star<br />
    <input type="checkbox" id="Star3"  name="Star3" value="3 Star" checked="checked" /> 3 Star<br />
    <input type="checkbox" id="Star4"  name="Star4" value="4 Star" checked="checked" /> 4 Star<br />
    <input type="checkbox" id="Star5"  name="Star5" value="5 Star" checked="checked" /> 5 Star+<br />
</form:form>
</div>

如果我错过了一些愚蠢的东西,任何帮助,甚至是滥用,都会受到赞赏。

5 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情:

$("input[type=checkbox]").click( function() {
    if ($(this).is(':checked')){
        alert('check');
    }
    else{
        alert('uncheck');
    }
});

您可以在此Fiddle

上试用

答案 1 :(得分:1)

尝试使用所有复选框。如果您想要执行任何特定复选框,请使用ID

$(':checkbox').click(function(){
    var el = $(this);
    if(!el.is(':checked')){
       alert ('unchecked');
       //if you want to trigger handler or do something. do it here
       el.triggerHandler('click');
   }
})

答案 2 :(得分:0)

试试这个javascript:

$().ready(function() {
    $(':checkbox').click(function() { alert($(this).attr('name')); });
});

答案 3 :(得分:0)

在此处查看工作演示jsfiddle。您可能忘记在页面中添加jquery js文件引用。

答案 4 :(得分:0)

但这很好......

<html>
  <head>
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function(){
        $('#filterStarDiv #hotelFilterForm #Star0').click( function() {
          alert('Checkbox 0 clicked');
        });
      });
    </script>
  </head>
  <body>
    <div id="starsRemoved" style="display:none">No Stars Removed</div>
    <div id="filterStarDiv">
      <h6>Click on the star ratings below to add or remove hotels in that category</h6>
      <form id="hotelFilterForm" action="" method="post">
        <input type="checkbox" id="Star0"  name="Star0" value="0 Star" checked="checked" /> 0 Star<br />
        <input type="checkbox" id="Star1"  name="Star1" value="1 Star" checked="checked" /> 1 Star<br />
        <input type="checkbox" id="Star2"  name="Star2" value="2 Star" checked="checked" /> 2 Star<br />
        <input type="checkbox" id="Star3"  name="Star3" value="3 Star" checked="checked" /> 3 Star<br />
        <input type="checkbox" id="Star4"  name="Star4" value="4 Star" checked="checked" /> 4 Star<br />
        <input type="checkbox" id="Star5"  name="Star5" value="5 Star" checked="checked" /> 5 Star+<br />
      </form>
    </div>
  </body>
</html>

找到你犯的错误。