我有一个带有AJAX函数的模板indexSuccess.php:
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#test<?php $mensajes->getIdmensajes() ?>').click(function(){
jQuery('#contenido<?php $mensajes->getIdmensajes() ?>').load(this.href);
return false;
});
});
</script>
<h2>Listado de mensajes emitidos</h2>
<h3>En orden cronológico inverso (más nuevo primero)</h3>
<table id="enviados" width="60%" border="1" cellpadding="8">
<thead>
<tr>
<th>Fecha Creación</th>
<th>Contenido del mensaje</th>
<th>Destinatarios</th>
</tr>
</thead>
<tbody>
<?php foreach ($mensajess as $mensajes): ?>
<tr>
<td width="10%" align="center"> <?php echo date('d/m/Y', strtotime($mensajes->getFechaalta())) ?></td>
<td bgcolor="<?php echo $mensajes->getColores()->getCodigo() ?>"><?php echo $mensajes->getCuerpo() ?></td>
<td width="20%" id="contenido<?php echo $mensajes->getIdmensajes() ?>">
<a id="test<?php echo $mensajes->getIdmensajes() ?>" href="<?php echo url_for('mensajes/receptores?idmensajes='.$mensajes->getIdmensajes()) ?>">Ver receptores</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
我想将值 $ message-> getIdmensajes (( 传递给 AJAX 函数。 我将为每行使用不同的ID,但这不起作用。但是当我设置值时,该函数运行良好。例如:jQuery ('# test24'( 和 jQuery ('# contenido24'( 适用于值 Idmensajes=24 。如何将值 $ message-> getIdmensajes (( 传递给 AJAX 函数?
你的问题不是很清楚,但你写了
jQuery ('#contenido24') works well for the value Idmensajes=24
另外,你有这个
jQuery('#test<?php $mensajes->getIdmensajes() ?>').click(function(){
jQuery('#contenido<?php $mensajes->getIdmensajes() ?>').load(this.href);
return false;
});
所以,我认为你有具有相似 id 的元素,例如 contenido24
、contenido25
等作为数据容器,以及与 id 的链接,如 #test24
、#test25
so。如果是这种情况,那么您可以简单地使用
jQuery(document).ready(function(){
// Register click handler for all a tags whose id begins with 'test'
jQuery("a[id^='test']").click(function(e){
e.preventDefault(); // instead of return false
jQuery('#contenido'+this.id.match(/'d+/)[0]).load(this.href);
});
});
jQuery('contenido'+this.id.match(/'d+/)[0])
将选择带有 id 的元素,如 #contenido24
,contenido25
取决于a
的ID
,如果一个a
标签有id='test20'
那么它将选择#contenido20
并将内容从 ajax 调用加载到这个元素中。