10

I have a problem, i have X <input type="checkbox" /> in my code, now I want to foreach this object/array its out put. - look my code.

$("#denied_seekrs").click(function()
{ 
    if (!isCheckedById("selectname")) 
    { 
        alert ("Please select at least one event"); 
        return false; 
    } 
    else 
    { 
        alert( $("input[@id=selectname]:checked").val() ); //submit the form 
    } 
}); 

function isCheckedById(id) 
{ 
    var checked = $("input[@id="+id+"]:checked").length; 
    if (checked == 0) 
    { 
        return false; 
    } 
    else 
    { 
        return true; 
    } 
}

When I output it in alert i get a object, but if I have select 2 checkbox I what the value in this 2 checkboxes.

I hope I can be helpful and all here understand me :)

Crisoforo Gaspar
  • 3,740
  • 2
  • 21
  • 27
ParisNakitaKejser
  • 12,112
  • 9
  • 46
  • 66
  • 3
    @NeoNmaN: There are useful spell checkers available for nearly every browser. Don't get me wrong, but I think you should install one. This question is really hard to understand. – Tomalak Sep 02 '09 at 09:27
  • You have to clarify your question. You want to get the value from all checked boxes? And what do you want todo with it? When you just submit the form, like you mention in the comment beside the alert, all checkbox values will be send to the server. – Tim Büthe Sep 02 '09 at 09:31

5 Answers5

29

How about

$("#denied_seekrs").click(function() {
    var checkedInputs = $("input:checked");
    var test = "";
    $.each(checkedInputs, function(i, val) {
        test += val.value+",";
    });
    test = test.substring(0,(test.length-1));
    alert(test);
});
George Brighton
  • 5,131
  • 9
  • 27
  • 36
william
  • 606
  • 3
  • 14
  • 29
3

I'm not exactly sure what you're looking for, but I'm guessing that the jQuery.each() method will help. You can use it to iterate over arrays, objects, and more.

var arr = [ "one", "two", "three", "four", "five" ];

jQuery.each(arr, function() {
     $("#" + this).text("My id is " + this + ".");
     return (this != "four"); // will stop running to skip "five"
});
2

how about something like this:

 jQuery.each(checked, function() {
      $(checked + this).text("My id is " + this + ".");

    });
william
  • 606
  • 3
  • 14
  • 29
2

Can it be that - ultimately - you are looking for $.serializeArray() or $.serialize()?

If not, then maybe this is helps you:

$("#denied_seekrs").click(function()
{ 
    if (!isCheckedById("selectname")) 
    { 
        alert ("Please select at least one event"); 
        return false; 
    } 
    else 
    { 
        // prepare array of values
        var values = [];

        // prepare list of checked checkboxes
        var $checkboxes = $("input[@id=selectname]:checked");

        // push each individual value into the array
        $checkboxes.each(function() { values.push( $(this).val() ); });

        // debug output
        alert( values.join("\n") ); 
        //submit the form 
    } 
});
Tomalak
  • 332,285
  • 67
  • 532
  • 628
0

When I got you right, you want the user to select one checkbox (or is it one or more?). This should do it:

$("#denied_seekrs").click(function()
{ 

    var $checkedInputs = $("input:checked"); 

    if ($checkedInputs.length != 1) 
    { 
        alert ("Please select one event"); 
        return false; 
    } 

    alert( $checkedInputs.val() ); //submit the form 
}); 

EDIT: After reading your question again, I realized that the above code does not answer your question. However, the above does work and is a much shorter version of your solution. Maybe you want to use it instead. To answer your question, you could alert the value of all checked boxes like this:

Change this:

    alert( $checkedInputs.val() ); //submit the form 

to this:

    var values = "";
    $checkedInputs.each(function(){
      values += $(this).val() + " ";
    });

    alert( values );
Tim Büthe
  • 62,884
  • 17
  • 92
  • 129
  • @NeoNmaN: Yes, I first got your question wrong. I just added an example to get all the values in the alert. – Tim Büthe Sep 02 '09 at 09:42