开发者

handle checkbox with class selector

开发者 https://www.devze.com 2023-03-08 14:41 出处:网络
I have a <asp:Checkbox /> and I want to see whether it\'s checked or not via jQuery. Problem is, it always returns false. Does it have to do with the fact that I\'m selecting the element via cla

I have a <asp:Checkbox /> and I want to see whether it's checked or not via jQuery. Problem is, it always returns false. Does it have to do with the fact that I'm selecting the element via class?

The js

$(document).ready(initialize);

var map;

function initializ开发者_StackOverflowe() {

    var x;
    x = $(".chkSetMap");
    x.click(setMap);
}

function setMap() {

    if ($('.chkSetMap').attr('checked') == true) {
        $(".comboMap").attr("disabled", true);

    }
}

the checkbox

    <asp:CheckBox ID="chkSetMap" CssClass="chkSetMap" runat="server" />

the checkbox rendered

<span class="chkSetMap"><input id="ctl00_ContentPlaceHolder1_chkSetMap" type="checkbox" name="ctl00$ContentPlaceHolder1$chkSetMap" /></span>


Try using the .is() method and the :checked shorthand selector

$(document).ready(initialize);

var $chkSetMap;

function initialize() {
    $chkSetMap = $(".chkSetMap input:checkbox").click(setMap);
}

function setMap() {
    $(".comboMap").attr("disabled", $chkSetMap.is(':checked'));
}

working example: http://jsfiddle.net/hunter/5XA7D/ UPDATED!


Since ASP.Net server controls are awesome it's wrapping your checkbox in a span. The span has the chkSetMap class, not the checkbox.


ASP.NET puts the CssClass on a <span> (which is where any Text would go). So, your selector picks up the <span>.

Try using the child selector to get to the checkbox: $('.chkSetMap > input:checkbox')


You can simply have an anonymous function. (#content is wjhatever div is the div on your page that has all the inputs you're looking for in it). This function will disable ALL of the dropdowns of .comboMap class if ANY of the checkboxes of chkSetMap is checked. If you only have 1 checkbox and one select - use IDs instead

$(document).ready(function() {

$(function() {
    $('.chkSetMap').click(function() {
        if ($(this).is(':checked')) {
            $("#content").find('.comboMap').attr("disabled", true);
        }else{
        $("#content").find('.comboMap').attr("disabled", false);
        }
    });

});
});
0

精彩评论

暂无评论...
验证码 换一张
取 消