简体   繁体   中英

Checkbox and JQUERY: “is checked” always FALSE

The code is below and checkbox is always FALSE. But on page it is different. It doesn't get checked or unchecked.

<script type="text/javascript">
 $('.cbOzelHastaAdi').unbind('click');

 $('.cbOzelHastaAdi').click( function() {

    var parentDiv = $(this).parent().get(0);
    var cbs = $(parentDiv).find('table input:checkbox');

   if($(this).attr("checked") === "true") {
        cbs.each(function() { $(this).attr('checked', false); });
    }
    else{
        cbs.each(function() { $(this).attr('checked', true); });
    }

})

</script>

One of the problems was that you had the checked attribute on the span surrounding the top checkbox and label and were binding an event handler to the span, therefore checked will always remain checked on the span.

I have moved the event handlers to the checkbox instead and tidied up the code a bit. Whilst I don't believe there is a problem in constructing your own attributes on HTML elements ie a checked attribute on a span element (I think XHTML strict validation fails with them), I don't whether it's considered good practice to do so.

I can see that you are using ASP.NET, based on the ID mangling - you can use server side <%= myControl.ClientID %> to get the mangled id to render in the HTML sent to the client.

Working Example here

   $(function() {     
        $('#rptOzel_ctl00_rptOzelHastalar_ctl00_cbOzelKurumHastasi').unbind('click');
        $('#rptOzel_ctl00_rptOzelHastalar_ctl00_cbOzelKurumHastasi').click( function() {

           var cbs = $('table input:checkbox');  

           if($(this).is(':checked')){
               cbs.each(function() { $(this).attr('checked', true); });
           }
           else{
            cbs.each(function() { $(this).attr('checked', false); });
           }

        });
    });

EDIT:

In response to your comment, you have a couple of options for resolving the clientid. If you write your jQuery in the aspx page, then you can simply use

$('#<%= cbOzelKurumHastasi.ClientID %>')

in place of

$('#rptOzel_ctl00_rptOzelHastalar_ctl00_cbOzelKurumHastasi')

If you have your jQuery in an external script file then you could put this in your aspx page

<script type="text/javascript">
var cbOzelKurumHastasi = '#<%= cbOzelKurumHastasi.ClientID %>';
</script>

and then use the variable in your external script file

$(function() {     
            $(cbOzelKurumHastasi).unbind('click');
            $(cbOzelKurumHastasi).click( function() { ...

For other options take a look at this question and answer - How to stop ASP.NET from changing ids in order to use jQuery

I usually us the .is() functionality of jQuery to check for this

$('.cbOzelHastaAdi').click( function() {
  if($(this).is(':checked')){
    ...
  }else{
    ...
  }
})

可能是因为你正在将checked属性的值与字符串而不是布尔值进行比较,并使用===运算符来比较值和类型?

I came across the same problem ones; checking whether a checkbox is checked ( in any of the different ways) always returned false.

My solution (course of problem) was in the JQuery selector that was used.

uzay95 used the class selector ".", to select its checkboxes.

$('.cbOzelHastaAdi')**

Which returns a result set. Therefor, checking if it is checked is invalid, because their might be multiple checkboxes... I know he's using the this keyword in his code, but for me this also did not fix the problem.

As soon as you select the checkbox by ID, you can evaluate its 'checked' attribute.

This is also working:

(($('input[name="myCheckBox"]:checked').val())=='on')

It will return either true or false

where the checkbox is defined as:

<input type="checkbox" name="myCheckBox">

I've found that the following is probably the best way to determine checked status in jQuery:

var cbs = $(parentDiv).find('table input:checkbox:checked');

This will give you an array of each input that has been checked.

This is mostly from the jQuery docs:

Selectors/Checked

Russ Cam's answer works, but why not jQueryify it some more?

$(function() {     
    $('.cbOzelHastaAdi').live('click', function() {
        $(this).parents('div').find('table input:checkbox').attr('checked', $(this).attr('checked'));
    });
});

This assumes that the class cbOzelHastaAdi is now attached to the checkbox instead of the span element. This should allow you to avoid all of the messy ASP renaming and allow multiple similar tables per page without the need for multiple click event functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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