【问题标题】:symfony 1.4 and jquery. How to pass variable to jquery function?symfony 1.4 和 jquery。如何将变量传递给jquery函数?
【发布时间】:2013-10-07 18:59:59
【问题描述】:

我有一个带有 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 函数?

【问题讨论】:

    标签: php jquery ajax symfony-1.4


    【解决方案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;
    });
    

    所以,我认为您有具有相似 id 的元素,例如 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]) 将根据aID 选择ID 为#contenido24contenido25 的元素,如果a 标记具有id='test20',那么它将选择#contenido20 并加载来自 ajax 的内容调用到这个元素中。

    【讨论】:

    • 问题解决了!他的解决方案效果很好。非常感谢你。你已经明白我想要什么了。再次非常感谢!
    猜你喜欢
    • 2012-03-02
    • 2011-01-18
    • 2012-06-19
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多