简体   繁体   English

jQuery和foreach循环

[英]JQuery and foreach loops

i have this code .. but with small problem .. i want to get some information about each module when i show the hidden tr .. i will get the information from the page {modules/$moduleid} .. i know how to use ajax .. but my problem is how to use the variable {$module.id} in the javascript code to give it the url of information page ! 我有此代码..但有一个小问题..当我显示隐藏的tr时,我想获取有关每个模块的信息..我将从页面{modules / $ moduleid}中获取信息..我知道如何使用ajax ..但我的问题是如何在JavaScript代码中使用变量{$ module.id}为它提供信息页面的url!

this is my code : 这是我的代码:

<script type="text/javascript">
$(document).ready(function(){

$('.subjects').click(function(){  
 var $tr = $(this).closest('tr');
$tr.nextUntil('.module-row').fadeIn(2000);
});

 $('.result').click(function(){  
 $(this).fadeOut(2000);
});
});
</script>
<br /><br /><table width="100%">
<tr class="tbl">
<td colspan="6">{$ci->lang->line('modules_list')}</td>
</tr><tr>
<td class="tbl1" style="width: 8%;">{$ci->lang->line('number')}</td>
<td class="tbl2" style="width: 28%;">{$ci->lang->line('title')}</td>
<td class="tbl1" style="width: 10%;">{$ci->lang->line('add_subject')}</td>
<td class="tbl2" style="width: 10%;">{$ci->lang->line('subjects_list')}</td>
<td class="tbl1" style="width: 8%;">{$ci->lang->line('edit')}</td>
<td class="tbl2" style="width: 8%;">{$ci->lang->line('delete')}</td>
</tr>
{foreach $modules_list as $module}
<tr class="module-row">
<td class="tbl1" style="width: 8%;">{$module.number}</td>
<td class="tbl2" style="width: 28%;">{$module.title}</td>
<td class="tbl1" style="width: 10%;"><a href="{base_url('admincp/subjects/add')}/{$module.id}"><img src="{base_url('images/icons/add.png')}" /></a></td>
<td class="tbl2" style="width: 10%;"><img style="cursor: url;" class="subjects" src="{base_url('images/icons/list.png')}" /></td>
<td class="tbl1" style="width: 8%;"><a href="{base_url('admincp/modules/edit')}/{$module.number}"><img src="{base_url('images/icons/edit.png')}" /></a></td>
<td class="tbl2" style="width: 8%;"><a onclick="return confirm('{$ci->lang->line('delete_confirm_msg')}')" href="{base_url('admincp/modules/delete')}/{$module.number}"><img src="{base_url('images/icons/delete.gif')}" /></a></td>
</tr>
<tr class="result" style="display:none">
<td colspan="7">
RESULT
</td>
</tr>
{/foreach}
</table>
</div>
</body>
</html>

for example this is the code i use to get the subjects of each module : 例如,这是我用来获取每个模块主题的代码:

 $.ajax({
   url: "../subjects/module_select/$module_id",
    type : "POST",
    data : dataAll,
    dataType :"html",
    success : function(msg){
       $('#select_subject').fadeIn(1000);
         $('#select_subject').html(msg)

    }

now how can i assign $module_id every time in the loop ! 现在如何在循环中每次分配$ module_id!

I think it's a mistake to not include the id in the table. 我认为在表格中不包含ID是错误的。 Here I've added $module_id to the data-id attribute of a hidden div. 在这里,我已将$ module_id添加到隐藏 div的data-id属性中。 This id is then assigned to moduleId , and you can then use that in your JS code any way you like. 然后将此ID分配给moduleId ,然后您可以按自己喜欢的任何方式在JS代码中使用它。

#module_id { display: none; }

<div id="module_id" data-id="$module.id"></div>

var moduleId = $('#module_id').data('id');

$.ajax({
  url: "../subjects/module_select/" + moduleId
});

Even better would be to add it to the data-id attribute on the element that' catching your click event, whatever that is. 更好的方法是将其添加到捕获您的click事件的元素的data-id属性中,无论如何。

<tr data-id="$module.id" class="module-row">

$(document).on('click', '.module-row', function () {
  var moduleId = $(this).data('id');
  // Do ajax
});

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

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