简体   繁体   English

Java中复选框的单击事件

[英]on Click event for checkbox in Javascript

I have a table with following format : 我有一个具有以下格式的表:

<tbody id="ggwp">
    <tr class="r123 disable-it">
        <td>
            <input  type="checkbox" name="item" class="row" statusid="1234">
        </td>
    </tr>   
    <tr class="r124">
        <td>
            <input  type="checkbox" name="item" class="row" statusid="1235">
        </td>
    </tr> 
    <tr class="r125 disable-it">
        <td>
            <input  type="checkbox" name="item" class="row" statusid="1236">
        </td>
    </tr>
</table>

Now, I want to call perform some action whenever user will click on checkbox of the rows with 'disable-it' set in the classname. 现在,只要用户单击在类名中设置了“ disable-it”的行的复选框,我就希望执行一些操作。 Also the event should be unfired when user will uncheck the checkbox. 当用户取消选中该复选框时,也应取消触发该事件。

Inshort, whenever user will click on checkbox of the rows with classnames ="r123 disable-it" and "r125 disable-it" ; 简而言之,每当用户单击具有类名=“ r123 disable-it”和“ r125 disable-it”的行的复选框时; some action must be performed and when user will unclick the checkbox, the event should be unfired/rolled back again. 必须执行一些操作,并且当用户取消单击该复选框时,应取消触发/回滚该事件。

How can I achieve this ? 我该如何实现? Thanks in advance for reading :) 预先感谢您的阅读:)

check if the check box row has class 'disable-it' and performs action 检查复选框行是否具有“ disable-it”类并执行操作

$(document).on("click",".row",function(e) {
    //Checks if the row has class disable-it
    if($(this).parent().parent().hasClass('disable-it')){
        if($(this).prop('checked')){
            //some action can be performed 
            alert('checked');
        }else{
            //unfired/rolled back again.
            alert('rollback');
        }
    }
});

Try this 尝试这个

$(".row").change(function() {

if(this.checked){
    //write your code
}
else{
    //write your code
   }
});

Check this fiddle 检查这个小提琴

$('input[type=checkbox]').click(function(){
    //alert('check');
      if($(this).is(":checked"))
      {
          $(this).parent().parent().addClass('disable-it');
      }
    else if(!$(this).is(":checked"))
      {
          $(this).parent().parent().removeClass('disable-it');
      }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM