admin管理员组

文章数量:1291253

I had a Checkbox I need to get the value of checkbox of while checked. either in JavaScript or jquery. I have placed my ASPX Code below. that Should Work in IE, While I am trying it is Showing response as "on" rather than value.

ASPX code:

<asp:TemplateField>
    <HeaderTemplate>
        <asp:CheckBox ID="CHK_STY_ALL" 
                      runat="server" 
                      onclick="javascript:Selectallcheckbox(this);" />
    </HeaderTemplate>
    <ItemTemplate>
        <asp:CheckBox ID="CHK_STY" 
                      runat="server" 
                      onclick="javascript:SelectallColorsForStyle(this,value);" 
                      CssClass="checkboxselection" 
                      Text='<%#Eval("STY_NBR")%>' />
    </ItemTemplate>
</asp:TemplateField>

JavaScript and JQuery :

<script language="javascript" type="text/javascript">
    function Selectallcheckbox(val) {
        if (!$(this).is(':checked')) {
            $('input:checkbox').prop('checked', val.checked);
        } else {
            $("#chkroot").removeAttr('checked');
        }
    }
</script>
<script type="text/javascript">
    function SelectallColorsForStyle(e,val) {
        var IDValue = $(e).attr('id');

        var StyleNumber = document.getElementById(IDValue).value;
        alert(StyleNumber);
    }
</script>

Showing value as "ON" rather than the original value displaying aside of checkbox.

I had a Checkbox I need to get the value of checkbox of while checked. either in JavaScript or jquery. I have placed my ASPX Code below. that Should Work in IE, While I am trying it is Showing response as "on" rather than value.

ASPX code:

<asp:TemplateField>
    <HeaderTemplate>
        <asp:CheckBox ID="CHK_STY_ALL" 
                      runat="server" 
                      onclick="javascript:Selectallcheckbox(this);" />
    </HeaderTemplate>
    <ItemTemplate>
        <asp:CheckBox ID="CHK_STY" 
                      runat="server" 
                      onclick="javascript:SelectallColorsForStyle(this,value);" 
                      CssClass="checkboxselection" 
                      Text='<%#Eval("STY_NBR")%>' />
    </ItemTemplate>
</asp:TemplateField>

JavaScript and JQuery :

<script language="javascript" type="text/javascript">
    function Selectallcheckbox(val) {
        if (!$(this).is(':checked')) {
            $('input:checkbox').prop('checked', val.checked);
        } else {
            $("#chkroot").removeAttr('checked');
        }
    }
</script>
<script type="text/javascript">
    function SelectallColorsForStyle(e,val) {
        var IDValue = $(e).attr('id');

        var StyleNumber = document.getElementById(IDValue).value;
        alert(StyleNumber);
    }
</script>

Showing value as "ON" rather than the original value displaying aside of checkbox.

Share Improve this question edited Dec 30, 2014 at 15:44 augustoccesar 6669 silver badges30 bronze badges asked Dec 30, 2014 at 14:52 VenkiVenki 5432 gold badges8 silver badges21 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

ASP.Net CheckBox is rendered as input and label.

<span class="checkboxselection">
    <input id="GridView1_CHK_STY_0" 
        type="checkbox" 
        name="GridView1$ctl02$CHK_STY" 
        onclick="javascript: SelectallColorsForStyle(this, value);" />
    <label for="GridView1_CHK_STY_0">100005</label>
</span>

So you need select sibling label's text.

<script type="text/javascript">
    function SelectallColorsForStyle(e, val) {
        var label = $(e).siblings("label");
        alert(label.text());
    }
</script>

I dont know the asp implimentation but this script will work for you.

<input type="checkbox" id="check-demo" value="some value" onchange="Selectallcheckbox(this)"/>

<script>
function Selectallcheckbox(element){
  alert(element.value);
}
</script>

if you are using jQuery then snippet below will work for you

$("#check-demo").click(function(){
 if($(this).is(":checked")) {
   alert($(this).val());     
   }
});

本文标签: Getting Checkbox in JavaScript or JQuery Value While OnClickStack Overflow