locked
Jquery val() help RRS feed

  • Question

  • User1489758560 posted

    Hi,

    I bound my database values in asp.net checkbox list and i am using jquery 1.10+ version. when i try to loop through the checkbox list values instead of values am getting 'on','on','on'... 

    below my sample code

    <asp:CheckBox ID="chksample" RepeatColumns="5" RepeatLayout="flow"
                                                    runat="server" RepeatDirection="Horizontal" />
    
    var strSample;
    
    $("[id*=chksample] input:checked").each(function () {
                var value = $(this).val();
                strSample += value + ", ";
            });
    
    

    Wednesday, May 4, 2016 4:49 AM

Answers

  • User61956409 posted

    Hi born2win,

    You could refer to the following sample code to get the selected value of checkboxes.

    <div>
        <asp:CheckBoxList ID="chksample" RepeatColumns="5" RepeatLayout="flow"
            runat="server" RepeatDirection="Horizontal" />
    </div>
    
    <script>
        $(function () {
            $("[id*='chksample']").change(function () {
                var strSample = "";
    
                $("[id*='chksample']:checked").each(function () {
                    strSample += $(this).val() + ",";
                });
    
                alert(strSample);
            })
        })
    </script>
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Value");
    
            dt.Rows.Add("1", "Item1");
            dt.Rows.Add("2", "Item2");
            dt.Rows.Add("3", "Item3");
    
            chksample.DataTextField = "Value";
            chksample.DataValueField = "ID";
            chksample.DataSource = dt;
            chksample.DataBind();
        }
    }
    

    Best Regards,

    Fei Han




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 5, 2016 4:55 AM

All replies

  • User-474980206 posted

    your code is working properly. "on" is the default value for a checkbox and you have not overriden it.

    Wednesday, May 4, 2016 5:01 AM
  • User1489758560 posted

    Hi, Bruce

    Thanks for the reply and in that case how can i get the value of checked items. any help please

    Wednesday, May 4, 2016 5:25 AM
  • User-369506445 posted

    hi

    your code is correct,

    but it depend , how to bind your data to checkboxlist

    for example try below code :

      var list1 = new ListItem("test1", "1");
                var list2 = new ListItem("test2", "2");

                var list = new List<ListItem> {list1, list2};

                chksample.DataSource = list;
                chksample.DataBind();
                for (int i = 0; i < chksample.Items.Count; i++)
                {
                    chksample.Items[i].Selected = true;

                }


    Wednesday, May 4, 2016 5:35 AM
  • User61956409 posted

    Hi born2win,

    You could refer to the following sample code to get the selected value of checkboxes.

    <div>
        <asp:CheckBoxList ID="chksample" RepeatColumns="5" RepeatLayout="flow"
            runat="server" RepeatDirection="Horizontal" />
    </div>
    
    <script>
        $(function () {
            $("[id*='chksample']").change(function () {
                var strSample = "";
    
                $("[id*='chksample']:checked").each(function () {
                    strSample += $(this).val() + ",";
                });
    
                alert(strSample);
            })
        })
    </script>
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Value");
    
            dt.Rows.Add("1", "Item1");
            dt.Rows.Add("2", "Item2");
            dt.Rows.Add("3", "Item3");
    
            chksample.DataTextField = "Value";
            chksample.DataValueField = "ID";
            chksample.DataSource = dt;
            chksample.DataBind();
        }
    }
    

    Best Regards,

    Fei Han




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 5, 2016 4:55 AM