简体   繁体   中英

hidden inputs with same name: how to send only the values of selected checkboxes?

We have a number of checkboxes linked to several hidden fields with the SAME NAME. How could we send only the values of the selected checkboxes?

php

<input type"checkbox" name="<?echo $filename;?>_<?echo $variable;?>"
id="filename_<?echo $variable;?>" value="1">

<input type="hidden" name="item_<?echo $filename;?>" 
value="<?echo $quantity;?>">

If two or more checkboxes with the same name are selected, how can we then obtain the correct value for each hidden input rather than always the value of the last one?

our generated html

<input type"checkbox" name="idfilename_1"
<input type="hidden" name="item_idfilename" value="7">

<input type"checkbox" name="idfilename_2"
<input type="hidden" name="item_idfilename" value="5">

<input type"checkbox" name="idfilename_3"
<input type="hidden" name="item_idfilename" value="11">

You can create an array like so:

<input type="checkbox" name="item_idfilename[]" value="7">
<input type="checkbox" name="item_idfilename[]" value="5">
<input type="checkbox" name="item_idfilename[]" value="11">

This way you'll get a list of checked checkboxes.

You could use array for both checkboxes and hiddens. Having checkboxes called check[1],check[2],check[3] and hiddens called item_hidden[1],item_hidden[2],etc.. you could relate every checkbox with a hidden input.

So your output will be:

<input type"checkbox" name="idfilename[1]" value="1">
<input type="hidden" name="item_idfilename[1]" value="7">

<input type"checkbox" name="idfilename[2]" value="1">
<input type="hidden" name="item_idfilename[2]" value="5">

<input type"checkbox" name="idfilename[3]" value="1">
<input type="hidden" name="item_idfilename[3]" value="11">

Server-side, you will cycle idfilename and, if value is set (So checkbox has been checked), you'll take value from item_idfilename array.

Normaly its done this way

<input type"checkbox" name="idfilename_1">
<input type="hidden" name="item_idfilename_1" value="7">

Only the checked once will have an field/index in the POST array.

On PHP side get the fields from the selected checkboxes.

If know for example an array_key exists eg idfilename_1 then you can take the value from $_POST['item_idfilename_1'] , if not leave it.

Keep in mind: Only selected checkboxes will have an field in the POST array!

Try this one :

  var $checkedInput = $('input[type=checkbox]:checked');
  if($checkedInput){
        var hiddenValue= $checkedInput.next('input[type=hidden]').val();
  }

--------------- or-------------------

 var $checkedInput= $('input[type=checkbox]:checked').next('input[type=hidden]')
                   .map(function() {
                         return $(this).val();
                   }).get();

you will get an array like ["7", "5", "11"] as per your code.

I used next() to get the next node value

 $('[type~=checkbox]').change(function(){ alert($(this).next().val()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" name="idfilename_1"> <input type="hidden" name="item_idfilename" value="7"> <input type="checkbox" name="idfilename_2"> <input type="hidden" name="item_idfilename" value="5"> <input type="checkbox" name="idfilename_3"> <input type="hidden" name="item_idfilename" value="11"> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM