admin管理员组

文章数量:1405115

I have a CheckBox control within a asp:Panel like below:

<asp:Panel ID="chk" runat="server" Visible="false" CssClass="checkbox checkbox-primary checkbox-single">
    <asp:CheckBox runat="server" ID="valCheck" />
</asp:Panel>

I need to be able to check to see if this CheckBox is being checked or not then I would send 0 or 1 to a function that will do some more stuff and when es back based on the value, if it's more than 0 it will display a checked check box. I need to use this value, because I also have some other types of inputs as well which I set their value based on the value ing back from another call.

var checking = false;
function saveCheckBoxData(obj, id, inputType) {
    if (checking) return;
    checking = true;
    var date = $("#MainContent_date").val();
    var column = $(obj).attr("column");
    var dataFieldId = $(obj).attr("dataFieldId ");
    var shift = $(obj).attr("shift");
    var val = 0;
    //if ($("#valCheck").is(':checked')) {
    //    val = 1;
    //}
    //if ($('#valCheck').prop('checked')) {
    //    val = 1;
    //}
    //if ($('#valCheck').attr("checked")) {
    //    val = 1;
    //}
    SaveInput(dataFieldId , shift, inputType, date, column, id, val);
}

When I call SaveInput function, I need to either pass 0 (if checkbox is not checked) or 1 (when it's checked). The lines I've mented out did not work!

Any feedback would be greatly appreciated!

I have a CheckBox control within a asp:Panel like below:

<asp:Panel ID="chk" runat="server" Visible="false" CssClass="checkbox checkbox-primary checkbox-single">
    <asp:CheckBox runat="server" ID="valCheck" />
</asp:Panel>

I need to be able to check to see if this CheckBox is being checked or not then I would send 0 or 1 to a function that will do some more stuff and when es back based on the value, if it's more than 0 it will display a checked check box. I need to use this value, because I also have some other types of inputs as well which I set their value based on the value ing back from another call.

var checking = false;
function saveCheckBoxData(obj, id, inputType) {
    if (checking) return;
    checking = true;
    var date = $("#MainContent_date").val();
    var column = $(obj).attr("column");
    var dataFieldId = $(obj).attr("dataFieldId ");
    var shift = $(obj).attr("shift");
    var val = 0;
    //if ($("#valCheck").is(':checked')) {
    //    val = 1;
    //}
    //if ($('#valCheck').prop('checked')) {
    //    val = 1;
    //}
    //if ($('#valCheck').attr("checked")) {
    //    val = 1;
    //}
    SaveInput(dataFieldId , shift, inputType, date, column, id, val);
}

When I call SaveInput function, I need to either pass 0 (if checkbox is not checked) or 1 (when it's checked). The lines I've mented out did not work!

Any feedback would be greatly appreciated!

Share Improve this question edited Sep 15, 2017 at 11:30 GLR 1,1201 gold badge11 silver badges31 bronze badges asked Jun 13, 2017 at 12:40 zezzzezz 932 silver badges12 bronze badges 4
  • 1 Check in browser that you have got same #valCheck as id to your checkbox – Guruprasad J Rao Commented Jun 13, 2017 at 12:43
  • 1 try $('<%=valCheck.ClientID %>').is(':checked') if you have written the js code in same aspx page. – Prashant Shirke Commented Jun 13, 2017 at 12:44
  • 1 I think asp change the id while sending the page to browser, so use ClientIDMode="static" in your checkbox field and then try. – Hassan Imam Commented Jun 13, 2017 at 12:47
  • @Prashant Shirke my js code is in a different file! – zezz Commented Jun 13, 2017 at 20:53
Add a ment  | 

2 Answers 2

Reset to default 3

The id is not exactly valCheck when you use runat="server"the element id changes(inspect the element to see the new id), then you need to use the jquery selector like:

$("select[id*='valCheck']")

or

$("select[id$='valCheck']")

or you can simply add the attr ClientIdMode="Static" in your asp element.

<asp:CheckBox runat="server" ID="valCheck" ClientIdMode="Static" />

If you want to understand why it happens, take a look at that article.

In plain javascript

 var isChecked= document.getElementById("myCheck").checked;

isChecked will be true if checked and false if not checked

本文标签: jqueryDetect whether a checkbox is checked or not in aspnet and javascriptStack Overflow