Pregunta

Así que soy nuevo en el uso de jquery .post(), sin embargo, no estoy usando métodos que no haya usado antes.

Estoy intentando publicar dos valores de entrada ocultos cuando se hace clic en un botón:

$('#button').live('click', function() {
    $.post('export_file.php', { group: form.group.value , test: form.test.value },
    function(output)    {
        $('#return').html(output).show();
    });
});

He probado que el evento del botón se activa correctamente y actualmente todo lo que intento hacer en export_file.php es eco de algo.

aquí está mi formulario:

<form name="form">
<input type="hidden" name="group" value="<?echo $group;?>">
<input type="hidden" name="test" value="<?echo $test_id;?>">
<input type="button" class="Mybutton" id="button" name="btnSubmit" value="Export Results">
</form>

tengo mi div en la página original:

<div id='return'></div>

export_file.php:

<?php

echo "whatever, something!";

?>

¿Alguien podría señalar dónde me estoy equivocando?Muchas gracias,

¿Fue útil?

Solución

Corrija esta línea:

$.post('export_file.php', { group: form.group.value , test: form.test.value },

Cámbielo a algo como esto:

var group_val = $('input[name="group"]', 'form[name="form"]').get(0).value;
var test_val = $('input[name="test"]', 'form[name="form"]').get(0).value;
$.post('export_file.php', { group: group_val , test: test_val },

Violín: http://jsfiddle.net/maniator/cQ2vZ/

Otros consejos

Intentar:

$('#button').live('click', function() {
    $.post('export_file.php', { group: $("input[name='group']").val() , test: $("input[name='test']").val() },
    function(output)    {
        $('#return').html(output).show();
    });
});

Agregué identificadores a los elementos de su formulario en su HTML:

<form name="form">
    <input type="hidden" name="group" id="group" value="<?echo $group;?>">
    <input type="hidden" name="test" id="test" value="<?echo $test_id;?>">
    <input type="button" class="Mybutton" id="button" name="btnSubmit" value="Export Results">
</form>

Luego modificó jQuery para obtener los valores de estos campos por ID y utilícelos en los parámetros de su llamada AJAX:

$('#button').live('click', function() {
    var groupValue = $("#group").val();
    var testValue = $("#test").val();

    $.post('export_file.php', { group: groupValue , test: testValue },
    function(output)    {
        $('#return').html(output).show();
    });
});

prueba este

$('#button').live('click', function() {
    var group_val = $("input[name='group']").val(); // gets the value of hidden field with the name group
    var test_val = $("input[name='test']").val(); // gets the value of hidden field with the name test and store it in test_val variable
    $.post('export_file.php', { group: group_val  , test: test_val  },
    function(output)    {
        $('#return').html(output).show();
    });
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top