Jump to content

Recommended Posts

Posted (edited)

Boa tarde,

Não sei como passar um array por ajax estou a utilizar o jquery será que alguem me pode ajudar.

Neste exemplo os valores nome e telefone consigo enviar acontece que nao consigo é enviar o array -> camposMarcados.

Ou seja neste caso quero enviar da checkbox o valor 100 e 165 que sao os que estao seleccionados.

eu tenho isto:

<script type="text/javascript">
$(document).ready(function(){

$('#enviar').click(function () {
var nome = $('#nome').val();
var telefone = $('#telefone').val();
camposMarcados = new Array();
$("input[type=checbox][name='ans[]']:checked").each(function(){
camposMarcados.push($(this).val());

var dataString = 'nome='+ nome + '&telefone=' + telefone+'&camposMarcados =' + camposMarcados;

$.ajax({
 type: "POST",
 url: "registar.php",
 data: dataString,
 success: function() {
 alert('Enviado');
 }
});

}); 	
});
</script>

<input type="text" name="nome" id="nome" />
<input type="text" name="telefone" id="telefone" />
<input type="checkbox" id="ans" name="ans[]" value="100" checked="checked">
<input type="checkbox" id="ans" name="ans[]" value="165" checked="checked">
<input type="checkbox" id="ans" name="ans[]" value="70">

<a href="javascript:void(0)" id="enviar">Enviar mensagem</a>
Edited by ricardocoimbra

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.