0

php我通过这样的循环 生成几种形式:

for($i=0 ; $i<count($recibidos) ; $i++){  
    $remitente = consultav2("SELECT * FROM mensajes JOIN ".$recibidos[$i]['u_remitente']." ON mensajes.remitente = ".$recibidos[$i]['u_remitente'].".id_".$recibidos[$i]['u_remitente']."
                                                    WHERE mensajes.id_mensaje = '".$recibidos[$i]['id_mensaje']."';");
    echo"
        <tr>
            <form id='recibido".$i."'>
            <td>".$remitente[0]['nombre']." ".$remitente[0]['apellidos']."</td><td>".$remitente[0]['asunto']."</td>
            <td>".$remitente[0]['fecha_mensaje']."</td><td><input type='submit' value='Leer' /></td>
            <input type='hidden' name='accion' value='leer' />
            <input type='hidden' name='id_mensaje' value='".$remitente[0]['id_mensaje']."' />
            </form>
        </tr>
    ";

}  

产生的form金额是不确定的。问题是我希望在单击相应按钮时form通过提交任何内容,但我在该部分中遇到问题。我不工作的建议是这样的...... jquery-ajaxsubmitjquery

$('[id=^recibido]').each(function(index, value){
  $(this).submit(function(){

    var datos = $(this).serialize();
    $.ajax({
        url: 'private/control_correo.php',
        type: 'post',
        data: datos,
        success: function(respuesta){
            $('#mostrar_mensaje').html(respuesta).show();
        },
        error: function(){
            $('#mostrar_mensaje').html('Error al intentar leer el mensaje').show();
        }
    });
    return false;
  });

}); 

jquery任何人都可以在我的代码中看到任何问题吗?我将不胜感激任何帮助。
提前致谢!

4

2 回答 2

1

您的属性选择器不正确...=^

$('[id=^recibido]').each(function(index, value){
//-----^^--here

它应该是^= jQuery( "[attribute^='value']" )

$('[id^="recibido"]').each(function(index, value){

而且您不需要这里的每个功能..只是

$('[id^="recibido"]').submit(function(){
  var datos = $(this).serialize();
    .......
   return false;
});

应该管用

于 2013-05-16T17:48:56.100 回答
1

您可能只想尝试对其进行一些优化。首先,给你的 php 生成的项目一个类:

<form id='recibido".$i."' class="recibidoForm">

然后,摆脱不必要的.each,只需使用.on()

$('.recibidoForm').on('submit',function(e){
    var datos = $(this).serialize(),
        $mostrar_mensaje = $('#mostrar_mensaje');

    e.preventDefault();

    $.ajax({
        url: 'private/control_correo.php',
        type: 'post',
        data: datos,
        success: function(respuesta){
            $mostrar_mensaje.html(respuesta).show();
        },
        error: function(){
            $mostrar_mensaje.html('Error al intentar leer el mensaje').show();
        }
    });
});

这应该做你想做的事,而且开销要少得多。此外,使用这样的类选择器比使用属性选择器更有效。

于 2013-05-16T17:51:37.923 回答