You need to access the checked box

I need to get the value that was assigned to a checkbox that was checked and I used to loop to show the checkboxes, now I need to access the values ​​of the checked checkboxes,

Can anyone help me with this?

foreach($inbox as $inbox_info) 
{
    <input type="checkbox" id="checkUnread" name="checkUnread" value="<? echo $inbox_info['inbox_id'];?>" />
}

      

I am trying to execute a mailbox function and I need to get the id of the items that have a checkbox so that I can mark those items that are not read in the database

0


a source to share


3 answers


To summarize what Everett said here, I don't think you really need the checkbox ID. If you set the value of each checkbox to the post id, you can do something like this:

<input type="checkbox" name="checkUnread" value="message_id" />

      

Then, to get an array of these values,



var vals = $.map($('input:checked'), function(e){ return $(e).val(); });

      

Check out the jsFiddle

+1


a source


Check :checked selector

$("input:checked").val()

      

Here's an example function.



function checkValue()
{
    $('.boxes:checked').each(function(){
        alert($(this).val());
    });
}

      

It works on this set.

<input type="checkbox" class="boxes" value="1" />
<input type="checkbox" class="boxes" value="2" />
<input type="checkbox" class="boxes" value="3" />
<input type="checkbox" class="boxes" value="4" />
<input type="checkbox" class="boxes" value="5" />
<input type="button" onclick="return checkValue()" value="Check Value" />

      

+6


a source


It looks like you want html needs.

Identity attributes in HTML must be unique - you are only allowed to use each identifier once per page. It looks like all your inputs are using the ID "checkUnread", this will throw errors in your Javascript (this probably also applies to the name, unless those values ​​are used when submitting the form).

I suggest you give your unique inbox_id as login id. Then, instead of getting the input value with Javascript, get the id.

0


a source







All Articles