简体   繁体   English

如何将多个具有相同类名的id值作为数组发送到Codeigniter中的数据库?

[英]How to send multiple id values with same classname to ajax as an array to the databse in codeigniter?

Hi guys i am trying to send multiple id with same classname but different id values to the database using ajax but it only picks the first values not all the values how can i send these multiple values. 嗨,大家好,我正在尝试使用ajax将具有相同类名但具有不同ID值的多个ID发送到数据库,但它仅选择第一个值,而不是所有值,我该如何发送这些多个值。

here is my code view 这是我的代码视图

<li><div class="hour_slots_available" id="01_02"><span class="hour_tag">01:00 - 02:00</span></div></li>
<li><div class="hour_slots_available" id="02_03"><span class="hour_tag">02:00 - 03:00</span></div></li>
<li><div class="hour_slots_available" id="03_04"><span class="hour_tag">03:00 - 04:00</span></div></li>
<li><div class="hour_slots_available" id="04_05"><span class="hour_tag">04:00 - 05:00</span></div></li>
<li><div class="hour_slots_available" id="05_06"><span class="hour_tag">05:00 - 06:00</span></div></li>
<li><div class="hour_slots_available" id="06_07"><span class="hour_tag">06:00 - 07:00</span></div></li>
<li><div class="hour_slots_available" id="07_08"><span class="hour_tag">07:00 - 08:00</span></div></li>
<li><div class="hour_slots_available" id="08_09"><span class="hour_tag">08:00 - 09:00</span></div></li>
<li><div class="hour_slots_available" id="09_10"><span class="hour_tag">09:00 - 10:00</span></div></li>

now on click 
<button class="btn btn-success" onclick="gethourprice();">Get Prices</button>

when this function calls i want the div with class hour slots available to got to the database as array if they are selected . 当此函数调用时,我希望具有班级时空的div(如果已选择)可以作为数组到达数据库。

here 这里

<script type="text/javascript">    
function gethourprice()
{    
var selected_ids = document.getElementsByClassName('hour_slots_available ').id;
alert(selected_ids);    
}
</script>

but when the alert calls the selected_ids only show a single id 12_01 what seems to be the problem here? 但是当警报调用selected_ids仅显示单个ID 12_01时,这似乎是问题所在? can you tell me ? 你能告诉我吗 ?

Try this: once you get all object with matching class, iterate the object and collect all ids, see below code 尝试以下操作:一旦获得具有匹配类的所有对象,请迭代该对象并收集所有ID,请参见以下代码

function gethourprice()
{    
var selectedIds="";
var selectedObject = document.getElementsByClassName('hour_slots_available ');
for(var i=0;i<selectedObject.length;i++)
  selectedIds+=selectedObject[i].id+",";
alert(selectedIds);    
}

You need to use loop here 您需要在这里使用循环

function gethourprice(){

    var selected_ids = document.getElementsByClassName('hour_slots_available ').id;
    for(var i = 0; i < selected_ids.length; i++) {
       alert(selected_ids[i]); // Will alert all ids in loop
   }
}

you can get all the value in array arrayDiv and now you can easily pass the array arrayDiv to ajax. 您可以获得数组arrayDiv所有值,现在您可以轻松地将数组arrayDiv传递给ajax。

Code ... 代码...

 var arrayDiv = new Array();; $('#getv').click(function(){ $('.hour_slots_available').each(function(i){ arrayDiv.push($(this).attr('id')); }); console.log(arrayDiv); // you can call Your ajax here }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li><div class="hour_slots_available" id="01_02"><span class="hour_tag">01:00 - 02:00</span></div></li> <li><div class="hour_slots_available" id="02_03"><span class="hour_tag">02:00 - 03:00</span></div></li> <li><div class="hour_slots_available" id="03_04"><span class="hour_tag">03:00 - 04:00</span></div></li> <li><div class="hour_slots_available" id="04_05"><span class="hour_tag">04:00 - 05:00</span></div></li> <li><div class="hour_slots_available" id="05_06"><span class="hour_tag">05:00 - 06:00</span></div></li> <li><div class="hour_slots_available" id="06_07"><span class="hour_tag">06:00 - 07:00</span></div></li> <li><div class="hour_slots_available" id="07_08"><span class="hour_tag">07:00 - 08:00</span></div></li> <li><div class="hour_slots_available" id="08_09"><span class="hour_tag">08:00 - 09:00</span></div></li> <li><div class="hour_slots_available" id="09_10"><span class="hour_tag">09:00 - 10:00</span></div></li> now on click <button id="getv" class="btn btn-success" >Get Prices</button> 

You should use something like this 你应该用这样的东西

function GetAllIds(){
    var idsArray = [];
    $(".hour_slots_available").each(function(){
        idsArray.push($(this).attr("id"));
        //or may be a comma seperated list
       //use jquery. write less do more :)
    });
    return idsArray;
}

but when the alert calls the selected_ids only show a single id 12_01 what seems to be the problem here? 但是当警报调用selected_ids仅显示单个ID 12_01时,这似乎是问题所在? can you tell me ? 你能告诉我吗 ?

Because document.getElementsByClassName returns an array of multiple strings, not a single string. 因为document.getElementsByClassName返回多个字符串的数组,而不是单个字符串。

Keep in mind that the code you shared returns undefined, as you are asking for the id value of an array. 请记住,当您要求数组的id值时,共享的代码返回的是未定义的。 So you need to remove the .id part in document.getElementsByClassName('hour_slots_available ').id , and loop over selected_ids to manipulate the individual strings. 因此,您需要删除document.getElementsByClassName('hour_slots_available ').id中的.id部分,并循环遍历selected_ids来处理各个字符串。

Example: 例:

function gethourprice() {
  var selected_ids = document.getElementsByClassName('hour_slots_available ');
  for (id in selected_ids) {
    alert(id);
  };
};
gethourprice();

CodePen demo (using console.log instead of alert): CodePen演示(使用console.log代替警报):

http://codepen.io/robee/pen/GWZxor http://codepen.io/robee/pen/GWZxor

getElementsByClassName function will to return array of elements. getElementsByClassName函数将返回元素数组。 but it was not javascript native function. 但这不是javascript本机功能。 get attribute from getElementsByClassName function will be depended by your browser. getElementsByClassName函数的get属性将取决于您的浏览器。 you need edit your gethourprice to get id form array of elements. 您需要编辑gethourprice以获取ID形式的元素数组。 here is example 这是例子

<script type="text/javascript">

    function gethourprice(){

        var selected_ids = document.getElementsByClassName('hour_slots_available');
        var ids = [];
        for (var i = 0;  i < selected_ids.length; i++) {
            ids.push(selected_ids[i].id)
        }
        alert(ids);
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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