English 中文(简体)
使用类选择器处理复选框
原标题:handle checkbox with class selector

我有一个<code><;asp:复选框/>,我想看看它是否通过jQuery进行了检查。问题是,它总是返回false。这与我通过类选择元素的事实有关吗?

js

$(document).ready(initialize);

var map;

function initialize() {

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

function setMap() {

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

    }
}

复选框

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

呈现的复选框

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

尝试使用.is()方法和:checked简写选择器

$(document).ready(initialize);

var $chkSetMap;

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

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

工作示例:http://jsfiddle.net/hunter/5XA7D/更新


由于ASP.Net服务器控件非常棒,它将您的复选框包装在span中。span具有chkSetMap类,而不是复选框。

问题回答

ASP.NET将CssClass放在<;span>(这是任何文本的位置)。因此,您的选择器选择<;span>

尝试使用子选择器以进入复选框:$(.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);
        }
    });

});
});




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!