2013-10-07 37 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

謝謝,歡迎:-) –