2013-10-07 40 views
0

我有模板indexSuccess.php的具有AJAX功能:symfony 1.4和jquery。如何将变量传递给jquery函数?

<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函数?

回答

1

你的问题不是很清楚,但你写

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; 
}); 

所以,我觉得你有类似的IDS元素,如contenido24contenido25等作为数据容器并链接到像#test24,#test25这样的ID。如果是这种情况,那么你可以简单地使用

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为元素,如#contenido24contenido25取决于aID,如果a标签有id='test20'然后它会选择#contenido20和加载内容ajax调用这个元素。

+1

问题解决了!他的解决方案效果很好。非常感谢你。你已经明白我想要的。再一次非常感谢你! –

+1

好的,我刚刚做到了。 –

+0

谢谢,欢迎:-) –