dongxun1244 2013-10-07 18:59
浏览 61
已采纳

symfony 1.4和jquery。 如何将变量传递给jquery函数?

I have the template indexSuccess.php with an AJAX function:

<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>

I want to pass the value $ message-> getIdmensajes () to the AJAX function. I will have a different ID for each row, but this does not work. But the function works well when I set the value. For example: jQuery ('# test24') and jQuery ('# contenido24') works well for the value Idmensajes=24 . How do I pass the value $ message-> getIdmensajes () to AJAX function?

  • 写回答

1条回答 默认 最新

  • doudouji2016 2013-10-07 19:48
    关注

    Your question is not so clear but you wrote

     jQuery ('#contenido24') works well for the value Idmensajes=24
    

    Also, you have this

    jQuery('#test<?php $mensajes->getIdmensajes() ?>').click(function(){
        jQuery('#contenido<?php $mensajes->getIdmensajes() ?>').load(this.href); 
        return false;
    });
    

    So, I think you have elements with similar ids, such as contenido24, contenido25 and so on as data container and links with ids like #test24, #test25 an so. If this is the case then you can simply use

    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]) will select elements with id like #contenido24, contenido25 depending on the ID of a, if an a tag has id='test20' then it'll select #contenido20 and load content from ajax call into this element.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行