Getting multiple selected checkbox values in a string in javascript and PHP

后端 未结 6 941
半阙折子戏
半阙折子戏 2021-01-02 21:21

I have location name and location Id in database table. Using foreach loop i\'m printing the values in checkbox in PHP. I have a submit button which triggers a javascript. I

相关标签:
6条回答
  • 2021-01-02 22:01
    var fav = [];
    $.each($("input[name='name']:checked"), function(){            
        fav.push($(this).val());
    });
    

    It will give you the value separeted by commas

    0 讨论(0)
  • 2021-01-02 22:08

    This is a variation to get all checked checkboxes in all_location_id without using an "if" statement

    var all_location_id = document.querySelectorAll('input[name="location[]"]:checked');
    
    var aIds = [];
    
    for(var x = 0, l = all_location_id.length; x < l;  x++)
    {
        aIds.push(all_location_id[x].value);
    }
    
    var str = aIds.join(', ');
    
    console.log(str);
    
    0 讨论(0)
  • 2021-01-02 22:10
    var checkboxes = document.getElementsByName('location[]');
    var vals = "";
    for (var i=0, n=checkboxes.length;i<n;i++) 
    {
        if (checkboxes[i].checked) 
        {
            vals += ","+checkboxes[i].value;
        }
    }
    if (vals) vals = vals.substring(1);
    
    0 讨论(0)
  • 2021-01-02 22:17

    This code work fine for me, Here i contvert array to string with ~ sign

        <input type="checkbox" value="created" name="today_check"><strong>&nbsp;Created&nbsp;&nbsp;</strong>
        <input type="checkbox" value="modified" name="today_check"><strong>&nbsp;Modified&nbsp;</strong>    
     <a class="get_tody_btn">Submit</a>
    
    <script type="text/javascript">
            $('.get_tody_btn').click(function(){
                var ck_string = "";
                $.each($("input[name='today_check']:checked"), function(){  
                    ck_string += "~"+$(this).val();  
                });
                if (ck_string ){
                    ck_string = ck_string .substring(1);
                }else{
                    alert('Please choose atleast one value.');
                }
    
    
            });
        </script>
    
    0 讨论(0)
  • 2021-01-02 22:20

    I you are using jQuery you can put the checkboxes in a form and then use something like this:

    var formData = jQuery("#" + formId).serialize();
    
    $.ajax({
          type: "POST",
          url: url,
          data: formData,
          success: success
    }); 
    
    0 讨论(0)
  • 2021-01-02 22:24

    In some cases it might make more sense to process each selected item one at a time.

    In other words, make a separate server call for each selected item passing the value of the selected item. In some cases the list will need to be processed as a whole, but in some not.

    I needed to process a list of selected people and then have the results of the query show up on an existing page beneath the existing data for that person. I initially though of passing the whole list to the server, parsing the list, then passing back the data for all of the patients. I would have then needed to parse the returning data and insert it into the page in each of the appropriate places. Sending the request for the data one person at a time turned out to be much easier. Javascript for getting the selected items is described here: check if checkbox is checked javascript and jQuery for the same is described here: How to check whether a checkbox is checked in jQuery?.

    0 讨论(0)
提交回复
热议问题