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

后端 未结 6 942
半阙折子戏
半阙折子戏 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: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);
    

提交回复
热议问题